Lowest Common Ancestor of a Binary Tree(二叉树公共祖先)
来源:https://leetcode.com/problems/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 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.
递归搜索,当root非空时,对其左右两子数分别进行搜索。若搜索结果都为非空,则两个节点分别位于左右两个子树中,LCA为root节点;若其中一个结果为空,则另一个不为空的为LCA;若两个结果都为空,则返回null
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
- if(root == null || (p == null && q == null)) {
- return null;
- }
- if(root == p || root == q) {
- return root;
- }
- TreeNode left = lowestCommonAncestor(root.left, p, q);
- TreeNode right = lowestCommonAncestor(root.right, p, q);
- if(left != null && right != null) {
- return root;
- } else if(left != null && right == null) {
- return left;
- } else if(left == null && right != null) {
- return right;
- } else {
- return null;
- }
- }
- }
Lowest Common Ancestor of a Binary Tree(二叉树公共祖先)的更多相关文章
- [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] 236. 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] 236. 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]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accordi ...
- [leetcode]236. 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 ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
随机推荐
- C#设置欢迎窗体由不透明变透明
public Form1() { InitializeComponent(); } private bool isForm1 = true; //设 ...
- SQL SERVER 索引维护
-- 全数据库索引重建 DECLARE @name varchar(100)DECLARE authors_cursor CURSOR FOR Select [name] from sysobject ...
- JavaScript秒针转换00:00:00代码
var str = realFormatSecond(e.target.currentTime); console.log(e.target.scrollTop); //1255256252 c ...
- LockSupport详解
concurrent包是基于AQS (AbstractQueuedSynchronizer)框架的,AQS框架借助于两个类: Unsafe(提供CAS操作) LockSupport(提供park/un ...
- MySQL基础day03 存储引擎和外键MySQL 5.6
MySQL基础day03_存储引擎和外键-MySQL 5.6 外键的条件: 1,表的存储引擎为innodb存储引擎 2,表中外键字段的类型要与参考表的字段类型一致 3,外键字段要是索引类型中的一种 M ...
- layui下拉框右边图标动画,不要动画
/* 取消动画 / .layui-form-selected .layui-edge { margin-top: -3px; -webkit-transform: rotate(0deg); tran ...
- Java——容器(泛型)
[泛型] 起因:JDK1.4之前类型不明确 <1>装入集合的类型都被当做Object对待,从而失去自己的实际类型. <2>从集合中取出时往往需要转型,效率低,且很容易出错 ...
- UE4中显示AI Debug信息
运行时,按下引号键('),就会出现AI的Debug信息,包含 AI Behavior Tree EQS Perception 四个大的分类,可以通过键盘上的1234键来显示和关闭相应的选项. 另外在E ...
- ES6 嵌套数组进行解构
let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 bar // 2 baz // 3 let [ , , third] = ["foo&qu ...
- HDU 6592 (LIS+输出字典序最大最小)
题意:给你一个序列,让你找长度最长的字典序最小和最大的单峰序列,单峰序列就是满足先增后降的序列. 思路:先正着求一遍LIS,再反着求一遍LIS,然后用单调栈来模拟. 求字典序最小的话,首先找到第一个顶 ...