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. 【DataStructure】Some useful methods about linkedList.

    /** * Method 1: Delete the input element x  * and meanwhile keep the length of array after deleted n ...

  2. Atitit.软件控件and仪表盘(23)--多媒体子系统--视频输出切换控制cvbs av s-video Ypbpr pal ntsc

    Atitit.软件控件and仪表盘(23)--多媒体子系统--视频输出切换控制cvbs av s-video Ypbpr pal  ntsc 1. CVBS是AV接口 1 2. S-Video S端子 ...

  3. Flume 中文入门手冊

    原文:https://cwiki.apache.org/confluence/display/FLUME/Getting+Started 什么是 Flume NG? Flume NG 旨在比起 Flu ...

  4. Python黑魔法,一行实现并行化

    Python 在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和 GIL,我觉得错误的教学指导才是主要问题.常见的经典 Python 多线程.多进程教程多显得偏“重”.而且往往隔靴搔 ...

  5. Android之ProGuard混淆器

    ProGuard是一个压缩.优化和混淆Java字节码文件的免费的工具,它可以删除无用的类.字段.方法和属性.可以删除没用的注释,最大限度地优化字节码文件.它还可以使用简短的无意义的名称来重命名已经存在 ...

  6. sass初学入门笔记(一)

    我本身是个新手,一边学sass一边记下的笔记,可能有点罗嗦,但是复习起来的话还是比较全面直观的.当然,最重要的还是去实践,实践得真理 其它 CSS 预处理器语言: CSS 预处理器技术已经非常的成熟, ...

  7. 存储管理器实验——SDRAM

    序言:2440有nand和nor两种启动方式,在裸机部分,都是使用的nand启动. 现在,我们想在nand flash启动的时候,通过SRAM访问存储在外设SDRAM中的程序. nand启动,先把前4 ...

  8. [C++]红色波浪线是什么意思

    相关资料:https://zhidao.baidu.com/question/242005953.html 问题现象:在写C++代码时,写的注释都是红色波浪线. 问题原因:波浪线表示 词语拼写错误 字 ...

  9. Python操作Word【批量生成文章】

    http://www.cnblogs.com/codex/p/4668396.html 需要做一些会议记录.总共有多少呢?五个地点x7个月份x每月4篇=140篇.虽然不很重要,但是140篇记录完全雷同 ...

  10. C语言 · 素数判断

     算法提高 素数判断   时间限制:1.0s   内存限制:512.0MB      编写一函数IsPrime,判断某个大于2的正整数是否为素数. 样例输入: 5样例输出:yes 样例输入: 9样例输 ...