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 ...
随机推荐
- JavaScript:Object.prototype.toString进行数据类型判定
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...
- firefox因 HTTP 严格传输安全(HSTS)机制无法打开网页
1.打开about:config 2.查找: security.enterprise_roots.enabled ,默认为false,改为true就可以了 3.吐槽,firefox太极端了,这是作死.
- atitit.提升研发管理的利器---重型框架 框架 类库的区别
atitit.提升研发管理的利器---重型框架 框架 类库的区别 1. 重型框架就是it界的重武器. 1 2. 框架 VS. 库 可视化图形化 1 3. 应用框架 1 4. 类库 2 5. 框架是不可 ...
- weblogic管理服务器密码相关
安全控制weblogic,我们可以为weblogic Administrator服务器设置密码 1.administrator服务器或受管服务器启动时,需要认证,方法有三种: (1)command启动 ...
- 裸的lcs
最长公共子串,裸的复杂度N^2 #include<bits/stdc++.h> using namespace std; ][]; int main() { ]; ]; scanf(&qu ...
- cocos2dx3.2升级Android5的坑
虽然已经转到服务端,但是对客户端的事情,偶尔还看看.公司的游戏用的是cocos2dx 3.2的版本, 然而在Android 5 上却无法运行. 先是没有触摸事件. 在stackoverflow上看到, ...
- 转:解决centos netstat和ps感染木马
解决方法:a.去除恶意文件的执行权限chmod 000 /tmp/gates.lod /tmp/moni.lod service sendmail stopchkconfig --level ...
- Java反射机制在工厂模式中的应用
在本篇文章中就不详细介绍工厂模式,主要介绍一下反射在工厂模式中的使用,让读者对反射机制带来的好处有更深的认识. 首先看一下简单工厂模式 简单工厂模式(simple factory)是类的创建模式,又叫 ...
- C/C++中printf和C++中cout的输出格式
一. Printf 输出格式 C中格式字符串的一般形式为: %[标志][输出最小宽度][.精度][长度]类型,其中方括号[]中的项为可选项.各项的意义介绍如下:1.类型类型字符用以表示输出数据的类型, ...
- Android基础总结(四)网络通信
网络图片查看器 确定图片的网址 发送http请求 URL url = new URL(address); //获取连接对象,并没有建立连接 HttpURLConnection conn = (Http ...