LeetCode-Lowest Common Ancestor of a Binary Tre
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.
/**
* 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的更多相关文章
- [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 ...
- [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 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
- [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 ...
- 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 ...
- 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. ...
- 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 ...
- 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. 思路 这一次 ...
- 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 ...
随机推荐
- Angularjs Directive - Compile vs. Link
如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应. 输入 hellobug 失去 ...
- OpenCV3+Python3
OpenCV3计算机视觉Python语言实现笔记 图像处理与OpenCV Python3与OpenCV3.3 图像处理 OpenCV文摘 基于Python3 + OpenCV3.3.1的远程监控程序 ...
- redis基础之订阅发布、主从复制和事务(四)
前面已经学习了redis的基本的命令行操作和数据类型,下面开始redis一些有趣的功能. 订阅和发布机制 定义:发布者相当于电台,订阅者相当于客户端,客户端发到频道的消息,将会被推送到所有订阅此频道的 ...
- PHPStorm 10 激活
按照这篇东东的说法去做已经不行了~ 可以参考我的另外一篇~ 传送门: http://www.cnblogs.com/gssl/p/5686612.html 楼主的图片看不到,下面是我找到的.分享出来. ...
- PHP截取中文字符串不出现?号的解决方法[原创]
PHP截取中文字符串不出现?号的解决方法[原创] 大 | 中 | 小 [不指定 -- : | by 张宴 ] [文章作者:张宴 本文版本:v1. 最后修改: 转载请注明出处:http://blog.z ...
- Mysql变量声明与使用
set @today='2017-04-25';set @ydate=DATE_SUB(@today, INTERVAL 7 day);select @today, @ydate; 待续....
- 利用Python操作Word文档【图片】
利用Python操作Word文档
- 知乎日报 API 分析
声明 下面全部 API 均由 知乎(Zhihu.Inc) 提供,本人採取非正常手段获取. 获取与共享之行为或有侵犯知乎权益的嫌疑.若被告知需停止共享与使用.本人会及时删除此页面与整个项目. 请您暸解相 ...
- resize2fs命令出现这个错误“resize2fs: Operation not permitted While trying to add group #6656” 有数据的会丢数据
1. resize2fs命令出现这个错误“resize2fs: Operation not permitted While trying to add group #6656”,并且在/var/log ...
- javascript -- 阻止默认事件 阻止事件冒泡
1. event.preventDefault(); -- 阻止元素的默认事件.注:a元素的点击跳转的默认事件 , button,radio等表单元素的默认事件 , div 元素没有默认事件 例: ...