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.

 
 Solution:
 /**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class Solution {
public class Result {
boolean findP, findQ;
TreeNode ancestor; public Result(boolean p, boolean q) {
findP = p;
findQ = q;
ancestor = null;
}
} public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
return findAncestorRecur(root, p, q).ancestor;
} public Result findAncestorRecur(TreeNode cur, TreeNode p, TreeNode q) {
if (cur == null) {
return new Result(false, false);
} boolean findP = (cur == p), findQ = (cur == q);
Result leftRes = findAncestorRecur(cur.left, p, q);
if (leftRes.ancestor != null)
return leftRes;
Result rightRes = findAncestorRecur(cur.right, p, q);
if (rightRes.ancestor != null)
return rightRes; findP = (findP || leftRes.findP || rightRes.findP);
findQ = (findQ || leftRes.findQ || rightRes.findQ); Result res = new Result(findP, findQ);
if (findP && findQ)
res.ancestor = cur; return res;
}
}

LeetCode-Lowest Common Ancestor of a Binary Tre的更多相关文章

  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] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

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

  3. LeetCode Lowest Common Ancestor of a Binary Tree

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

  4. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

  5. [LeetCode]Lowest Common Ancestor of a Binary Search Tree

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

  6. LeetCode Lowest Common Ancestor of a Binary Serach Tree

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

  7. Leetcode ——Lowest Common Ancestor of a Binary Tree

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

  8. LeetCode——Lowest Common Ancestor of a Binary Search Tree

    Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...

  9. 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. 思路 这一次 ...

  10. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

随机推荐

  1. XML文件标签名一致,而属性值不同,如何遍历取值写法 摘录

    <EssentialFunctions>      <Qualification description="We Offer" source="AdDe ...

  2. 邁向 RHCE 之路 (Day26) - Apache 網頁伺服器

    本篇將在 SELinux 安全機制及 IPTables 防火牆開啟的環境下實作,分別實作簡單網頁服務及虛擬主機 Virtual Host 設定,最後則是實作網頁中需要保護網頁時可以透過 .htacce ...

  3. dbrd 8.4.6 源代码编译安装

    ---------------------------- 0.系统环境 ---------------------------- db01 192.168.50.10 /dev/sdb1 主节点 db ...

  4. location 设定某个文件的过期时间,并不记录访问日志

    网页的根目录本来是: 6 root /app/www/default; [root@web01 default]# cat /app/server/nginx/conf/vhosts/default. ...

  5. 每日英语:How to find the career of your dreams

    The fate described by Dostoyevsky is a nightmare we all hope to escape. But we're surrounded by nays ...

  6. am335x 一个按键实现重置 ip 和 root passwd

    * 其实做法很简单,我连按键驱动都没有去写,读取 gpio 的值然后 拷贝了一份 /etc/network/interfaces_bak 为 interfaces ,用脚本重新设置了一次root 密码 ...

  7. 机器学习:如何通过Python入门机器学习

    我们都知道机器学习是一门综合性极强的研究课题,对数学知识要求很高.因此,对于非学术研究专业的程序员,如果希望能入门机器学习,最好的方向还是从实践触发. 我了解到Python的生态对入门机器学习很有帮助 ...

  8. selenuim爬虫实战 (下)

    SuperLOFTERDownloader7.java package test; import java.io.IOException; import java.util.ArrayList; im ...

  9. javascript弹出层-DEMO001

    首先上一张图 看下弹出层的效果 从图中可以看到二部分 一是弹出层 二是遮照层 弹出层:即弹出你要操作的内容 遮照层:遮照住不要操作的内空 实际技术原理主要是 CSS +JS  (z-index是核心) ...

  10. 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...