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).”

  1. _______3______
  2. / \
  3. ___5__ ___1__
  4. / \ / \
  5. 6 _2 0 8
  6. / \
  7. 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.

找最近的公共祖先,仔细想一想,其实l与r的最小的公共祖先c满足一定条件,那就是l以及r一定在c的左右分支上,不可能都是左或右分支的。否则一定就不是最近的公共祖先,

代码如下,用递归写出来还是比较简单易懂的。

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  13. if (root == NULL) return NULL; //无其他节点,直接返回
  14. if (root == p || root == q) return root;
  15. TreeNode * leftNode = lowestCommonAncestor(root->left, p, q);
  16. TreeNode * rightNode = lowestCommonAncestor(root->right, p, q);
  17. if (leftNode && rightNode) return root;  //找到LCA,返回LCA
  18. return leftNode ? leftNode : rightNode;  
  19. }
  20. };

还有一种方法是遍历tree,然后找出到达p以及q分别的路径,找到路径之后,遍历两条路径,出现分叉的第一个点就是p与q的LCA。具体代码先不贴了  比较麻烦。

java版本的如下所示,方法相同:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  12. if(root == null)
  13. return null;
  14. if(root == p || root == q)//切断路径,不再向下执行了
  15. return root;
  16. TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
  17. TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
  18. if(leftNode != null && rightNode != null)
  19. return root;
  20. if(leftNode!=null) return leftNode;
  21. if(rightNode != null) return rightNode;
  22. return null;
  23. }
  24. }

LeetCode OJ:Lowest Common Ancestor of a Binary Tree(最近公共祖先)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...

  4. 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 ...

  5. (medium)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 ...

  6. [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. Accordi ...

  7. [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 ...

  8. LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree

    解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...

  10. [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先

    如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...

随机推荐

  1. 分布式计算hadoop三大组件

    设计原则:移动计算,而不是移动数据 计算层:Map/Reduce调度层:YARN数据层:HDFS 这三层之间没有必然的依赖性,只是经常这么搭配,而且都是hadoop那个包里一起安装的,三层都可以独立运 ...

  2. 你真的会用Retrofit2吗?Retrofit2完全教程

    本文注目录: Retrofit入门 Retrofit注解详解 Gson与Converter RxJava与CallAdapter 自定义Converter 自定义CallAdapter 其它说明 前言 ...

  3. C# 调用win api获取chrome浏览器中地址

    //FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...

  4. SHELL —— grep命令+正则表达式

    一 什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 生活中处处都是正则: 比如我们描述:4条腿 你可能会想 ...

  5. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  6. Loadrunder场景设计篇——IP欺骗

    适用协议 LoadRunner的多ip功能允许运行在单一负载生成器上的Vuser可以通过多ip被识别.服务器和路由识别这些vuser为来自不同负载生成器上.   2  在负载生成器(load gene ...

  7. ado.net(增删改)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Linux yum源地址

    ----------------------------------Linux yum源地址------------------------------ Zabbix 3.0 yum源 rpm -iv ...

  9. 外部类与main方法笔记

    外部类 1. 外部public class只能有一个 2. 外部类只能有两种访问控制级别: public 和默认 3. 一个文件中,可以有多个public class,即外部类为public,还可以有 ...

  10. NOIP 能量项链

    描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定等于 ...