来源:https://leetcode.com/problems/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 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.

递归搜索,当root非空时,对其左右两子数分别进行搜索。若搜索结果都为非空,则两个节点分别位于左右两个子树中,LCA为root节点;若其中一个结果为空,则另一个不为空的为LCA;若两个结果都为空,则返回null

  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. class Solution {
  11. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  12. if(root == null || (p == null && q == null)) {
  13. return null;
  14. }
  15. if(root == p || root == q) {
  16. return root;
  17. }
  18. TreeNode left = lowestCommonAncestor(root.left, p, q);
  19. TreeNode right = lowestCommonAncestor(root.right, p, q);
  20. if(left != null && right != null) {
  21. return root;
  22. } else if(left != null && right == null) {
  23. return left;
  24. } else if(left == null && right != null) {
  25. return right;
  26. } else {
  27. return null;
  28. }
  29. }
  30. }

Lowest Common Ancestor of a Binary Tree(二叉树公共祖先)的更多相关文章

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

  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 二叉树的最近公共祖先

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

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

  5. [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. 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...

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

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

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

  9. 88 Lowest Common Ancestor of a Binary Tree

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

  10. 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 利用二 ...

随机推荐

  1. C#设置欢迎窗体由不透明变透明

    public Form1()        {            InitializeComponent();        } private bool isForm1 = true;  //设 ...

  2. SQL SERVER 索引维护

    -- 全数据库索引重建 DECLARE @name varchar(100)DECLARE authors_cursor CURSOR FOR Select [name] from sysobject ...

  3. JavaScript秒针转换00:00:00代码

    var str  = realFormatSecond(e.target.currentTime);   console.log(e.target.scrollTop); //1255256252 c ...

  4. LockSupport详解

    concurrent包是基于AQS (AbstractQueuedSynchronizer)框架的,AQS框架借助于两个类: Unsafe(提供CAS操作) LockSupport(提供park/un ...

  5. MySQL基础day03 存储引擎和外键MySQL 5.6

    MySQL基础day03_存储引擎和外键-MySQL 5.6 外键的条件: 1,表的存储引擎为innodb存储引擎 2,表中外键字段的类型要与参考表的字段类型一致 3,外键字段要是索引类型中的一种 M ...

  6. layui下拉框右边图标动画,不要动画

    /* 取消动画 / .layui-form-selected .layui-edge { margin-top: -3px; -webkit-transform: rotate(0deg); tran ...

  7. Java——容器(泛型)

    [泛型]  起因:JDK1.4之前类型不明确  <1>装入集合的类型都被当做Object对待,从而失去自己的实际类型.  <2>从集合中取出时往往需要转型,效率低,且很容易出错 ...

  8. UE4中显示AI Debug信息

    运行时,按下引号键('),就会出现AI的Debug信息,包含 AI Behavior Tree EQS Perception 四个大的分类,可以通过键盘上的1234键来显示和关闭相应的选项. 另外在E ...

  9. ES6 嵌套数组进行解构

    let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 bar // 2 baz // 3 let [ , , third] = ["foo&qu ...

  10. HDU 6592 (LIS+输出字典序最大最小)

    题意:给你一个序列,让你找长度最长的字典序最小和最大的单峰序列,单峰序列就是满足先增后降的序列. 思路:先正着求一遍LIS,再反着求一遍LIS,然后用单调栈来模拟. 求字典序最小的话,首先找到第一个顶 ...