首页 > 其他 > 详细

Leetcode 865. Smallest Subtree with all the Deepest Nodes

时间:2018-07-15 21:48:01      阅读:417      评论:0      收藏:0      [点我收藏+]

这道题的关键在于知道每个节点的深度,可以用哈希表保存的前提下,怎么找出公共最小的父节点。方法是,如果当前node左右节点都有深度最大节点,返回当前node,如果只有左边有,返回node的左节点,反之返回右节点。也就是以下的java代码
public TreeNode answer(TreeNode node) {
if (node == null || depth.get(node) == max_depth)
return node;
TreeNode L = answer(node.left),
R = answer(node.right);
if (L != null && R != null) return node;
if (L != null) return L;
if (R != null) return R;
return null;
}

Leetcode 865. Smallest Subtree with all the Deepest Nodes

原文:https://www.cnblogs.com/bloomingFlower/p/9314903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!