LeetCode 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.
题解:
是Lowest Common Ancestor of a Binary Search Tree进阶题目。
无法比较大小,但是可以看p,q是不是在root的两边,若在两边,left 和 right 同时不失null, 则返回root.
若都在一边,比如left, 就在left边继续。
Note:1. 若有root == p || root == q时,需比较原来的点而不单单是val, 这里可以有重复的值,在能比较整体点时就不比较val.
2. 递归终止条件这里有两个,一个是root == null, 一个是root等于p或者q, 这两个终止条件缺一不可。
Time Complexity: O(n), 每个点没有traverse超过两遍. Space: O(logn), 是树的高度。
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){
return root;
}
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){
return left;
}else{
return right;
}
}
}
跟上Smallest Subtree with all the Deepest Nodes.
LeetCode 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 ——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 Tree
题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- [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之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 ...
- 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 利用二 ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
随机推荐
- Spring动态配置多数据源
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...
- WampServer搭建php环境时出现的哪些问题?
WampServer搭建php环境时遇到的问题 安装时报错,缺少MSVCR100.dll文件 这是因为wampServer安装时用到的vc库没有更新,要安装更新之后再进行安装,因为之前安装的VC版本低 ...
- 根据PHP手册什么叫作变量的变量?
在最近做的一个项目中,发现了一个新的概念,关于在PHP中使用变量的变量.在的程序中,需要在一个页面同时更新多个记录,在经过相当长时间的痛苦思索之后,脑海中偶然地闪现出了变量的变量(variable v ...
- Stackoverflow架构
Stackoverflow用的是.net开发的,用的缓存是Redis,Stackoverflow架构的演讲地址是:http://www.infoq.com/cn/presentations/archi ...
- java---一元二次方程练习
public class wu{ public static void main(String[] args){ int a = 2,b = 1, c = 0,d = b*b-4*a*c if (a ...
- 【液晶模块系列基础视频】2.虚拟U盘
[液晶模块系列基础视频]2.虚拟U盘 ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee ...
- CSS3:动画大全
和过渡的区别 页面不用明显js调用: 过渡:必须有:hover visited 等伪类调用.(本质还是事件驱动) 动画:页面加载上就可以. 页面有js调用: 7个参数,*为可选 animation-n ...
- Alamofire数据请求
let stringurl = "http://www.huiyunche.cn/kyleuat/banner/list" Alamofire.request(.GET,strin ...
- PhpStorm中文教程
PhpStorm中文教程 | 浏览:15972 | 更新:2014-06-10 21:14 1 2 3 4 5 分步阅读 PhpStorm是一款强大的IDE,非常适合于PHP开发人员及前端工程师.提供 ...
- shell 中的数学计算
1.1.第一种——expr格式:expr 操作数 1 操作符 操作数 2举例: 1 expr 1 + 2 TMP=$(expr 1 + 2) 2 expr 1 + 4 / 3 TM ...