码迷,mamicode.com
首页 > 其他好文 > 详细

fb: Lowest Common Ancestor

时间:2017-12-13 02:14:27      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:contain   etl   log   integer   lca   hat   class   root   body   

/**
# Given the following tree:
        a
      / |     b   c   d
   / \      |
  e   f     g
/   / \   
h   i   j   

depth of tree: 4
deepest nodes: [h, i, j]
least common ancestor of [h, i, j]: b


Given a tree, find the smallest subtree that contains all of the tree‘s deepest nodes. (ie, find the LCA of the deepest nodes)
*/

TreeNode {
  value: int,
  childern: List<TreeNode>,
  parent: TreeNode,
}

public TreeNode rootOfTheSmallestSubTreeForAllDeepestNodes(TreeNode root) {

}

TreeNode res =
// nodes=[h, i, g], ancestor={h: e, i: f, j: f, etc}
public TreeNode getLowestCommonAncestor(List<TreeNode> nodes) {
    int num = nodes.length();
    Map<TreeNode, Integer> map = new HashMap<>();
    for (TreeNode node : nodes) {
        pass(map, node, num);
    }


}

public void pass (Map<TreeNode, Integer> map, TreeNode node, int num) {

}




public void getDeepNodes (TreeNode root, int[] depth, int currDepth, Set<TreeNode> set) {
    if)

}

用 map 记录深度

fb: Lowest Common Ancestor

标签:contain   etl   log   integer   lca   hat   class   root   body   

原文地址:http://www.cnblogs.com/apanda009/p/8030366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!