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 ...
随机推荐
- 网站建设中HTTP状态码的奥秘
在网络营销中,站长经常会遇到一些HTTP状态码的问题,不懂HTTP状态码那么做SEO优化就无从谈起,下面是脉凌网络对HTTP状态码总结的一览表. 1xx:请求收到,继续处理 2xx:操作成功收到,分析 ...
- python 面向对象的三大特征之 封装
封装:私有化 class Person(object): def __init__(self): self.__gender = "man" #在类的属性名称前面加__ self. ...
- Scala - Spark Lambda“goesto“ => 分析
/// 定义一个函数AddNoise,参数分别为rdd,Fraction.其中rdd为(BreezeDenseMatrix, BreezeDenseMatrix)元组构成的RDD.Fraction为一 ...
- 常用的一些webshell木马官方后门
80SEC剑心修改过世界杀毒版 http://wwwxx.cn/1.asp?web2a2dmin=//?web2a2dmin=//1.asp 绕过 不灭之魂的后门 1.在错误页面右键可以查看密码 2 ...
- java web工程之Hibernate
java web添加structs特性后再添加Hibernate特性,这可以通过右键工程->my eclipse出现工具条选中相应的条目,添加相应的属性, 添加完Hibernate后建立与数据库 ...
- Css - Table.css
基本的Table样式 .gridtable { border: solid #ccc 1px; -moz-border-radius: 6px; -webkit-border-radius: 6px; ...
- Javascript - 数组去重复
这里我使用的场景是将表单中所有的input的值塞入数组中,然后通过去除重复的值.如果数组的长度和原数组的长度一致,说明没有重复,如果不一致(少于)则报错 //通过$.unique对数组进行“去重”,再 ...
- X-Japan
听X Japan这么久,几位大叔还是没有认清,真是惭愧. X-Japan是日本著名的视觉系摇滚乐队.原来叫X,在1992年8月HEATH入团的同时改名为X JAPAN. 乐队成立于1982年1月,19 ...
- Java学习记录-注解
注解 一.org.springframework.web.bind.annotation ControllerAdviceCookieValue : 可以把Request header中关于cooki ...
- Redis 笔记与总结2 String 类型和 Hash 类型
Linux 版本信息: cat /etc/issue 或cat /etc/redhat-release(Linux查看版本当前操作系统发行版信息) CentOS release 6.6 (Final) ...