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 ...
随机推荐
- C/C++:C++伪函数
C++伪函数: 所谓的伪函数.就是说它不是一个真正的函数,而是一个类或者说是一个结构体. <span style="font-size:18px;"> #include ...
- 50篇经典珍藏 | Docker、Mesos、微服务、云原生技术干货
概念篇 全方位探(tian)索(keng)Mesos各种存储处理方式 老肖有话说@Mesos User Group第四次约会 技术实践 | Mesos 全方位“烹饪”指南 回顾 JAVA 发展轨迹,看 ...
- 源码分析HotSpot GC过程(二):DefNewGeneration的GC过程
由于虚拟机的分代实现,虚拟机不会考虑各个内存代如何实现垃圾回收,具体的工作(对象内存的分配也是一样)由各内存代根据垃圾回收策略自行实现. DefNewGeneration的使用复制算法进行回收.复制算 ...
- Oracle 11g客户端中文乱码问题
问题现象: 客户端安装完毕后,访问表内容,发现中文显示的都是?? 解决方案: 1.修改客户端注册表,路径为HKEY_LOCAL_MACHINE --->SOFTWARE ---> ORAC ...
- iOS开发-iOS 10 由于权限问题导致崩溃的那些坑
iOS开发-iOS 10 由于权限问题导致崩溃的那些坑 6月份的WWDC大会结束有一段时间了,相信很多开发者也是在努力工作的闲时用着Xcode8 Beta版学习着新的特性吧. 使用Xcode8写自己 ...
- The difference between Severity and Priority
The difference between Severity and Priority[1] 2015-06-24 There are two key things in defects of th ...
- 基于jdom 的 xmluti
package cn.com.do1.wechat.common; import org.jdom.Attribute; import org.jdom.Document; import org.jd ...
- 用Netty开发中间件:高并发性能优化(转)
用Netty开发中间件:高并发性能优化 最近在写一个后台中间件的原型,主要是做消息的分发和透传.因为要用Java实现,所以网络通信框架的第一选择当然就是Netty了,使用的是Netty 4版本.Net ...
- php7 安装扩展
安装mongodb扩展 前提:安装好php7,位置:/usr/local/php 方法一: /usr/local/php7/bin/pecl install mongodb service php-f ...
- C++ 类的抽象初练
/* 某商店经销一种货物,货物的购进和卖出以箱为单位,各箱的重量不一样, 因此商店需要目前库存的总重量. 现在用c++模拟商店货物购进和卖出的情况 */ #include<iostream> ...