Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

题目大意:求二叉树的两个节点的最近祖先。

解题思路:递归的来写,

设两个变量left和right,如果在root节点的左右孩子节点分别发现节点p和q,

那么假设left=q,right=p

那么root就是最近祖先;

如果left=null,right=q或p,那么right就是最近祖先;

如果right=null,left=q或p,那么left就是最近祖先。

  public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null||root==q||root==p){
return root;
}
TreeNode left = lowestCommonAncestor(root.left,p,q);
TreeNode right = lowestCommonAncestor(root.right,p,q);
if(left!=null&&right!=null){
return root;
}
return left==null?right:left;
}

也看到有人用map记录父节点信息,然后利用map找出最近公共祖先,也不错。

代码如下:

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
HashMap<TreeNode,TreeNode> m = new HashMap<TreeNode,TreeNode>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(queue.peek()!=null){
TreeNode t = queue.poll();
if(t.left!=null){
m.put(t.left,t);
queue.offer(t.left);
}
if(t.right!=null){
m.put(t.right,t);
queue.offer(t.right);
}
}
Set<TreeNode> l = new HashSet<TreeNode>();
TreeNode pp = p;
while(pp!=root){
l.add(pp);
pp = m.get(pp);
}
l.add(root);
TreeNode qq = q;
while(!l.contains(qq)){
qq = m.get(qq);
}
return qq;
}

Lowest Common Ancestor of a Binary Tree——Leetcode的更多相关文章

  1. Lowest Common Ancestor of a Binary Tree leetcode

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  2. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  3. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  4. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  5. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  6. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  7. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  8. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  9. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. 标准web架构分层

    标准Web系统的架构分层 转载:http://blog.csdn.net/yinwenjie    http://blog.csdn.net/yinwenjie/article/details/464 ...

  2. fmt:formatDate标签的输出格式

    http://blog.csdn.net/lidawei201/article/details/7197834

  3. 类库探源——System.Delegate

    一.MSDN 描述 Delegate 类:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法.(是不是感觉很像C语言中的函数指针 :) ) 命名空间: System 程序集:   ...

  4. 标准的最大margin问题

    standard large margin problem 分割线

  5. X-Plane飞行模拟资源整理一

    计划开一个博客整理一下飞行仿真软件二次开发的相关内容 预计将陆续介绍X-Plane.Microsoft Flight Simulator.FlightGear三个主流飞行模拟器. 此处为目录(占坑,随 ...

  6. Eclipse Git和sourceTree用法

    Eclipse Git和sourceTree用法 Eclipse Git: 提交代码到git: 1.team->Repository->pull 若没有冲突: 2.team->com ...

  7. Asp.net服务器控件在IE10下的不兼容问题

    Asp.net服务器控件在IE10下的不兼容问题 时间:2013-05-16 09:07点击: 89 次 [大 中 小] 相信很多使用IE10的童鞋们已经发现了这个问题,以下是本人在IE10标准模式下 ...

  8. ip输入框键入.或者合法数字自动选择下一个输入框效果

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  9. Windows下面对环境变量的操作

    如何在cmd命令行中查看.修改.删除与添加环境变量:首先明确一点:所有的在cmd命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改.也就是说当关闭此cmd命令行窗口后,将不再起作用.永久性修改 ...

  10. phpcms v9使用GET调用指定id文章内容、页面数据方法

    不知道大家有没有相同的体会?在使用Phpcms V9建站调用数据的时候,基础的数据用默认的模板的调用语句就好了,但复杂不常见的数据怎么调用呢?我们技术可能会研究半天,怀着探索的精神不断尝试.孜孜不倦. ...