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 ...
随机推荐
- Centos下设置静态IP
使用VirtualBox装了很多虚拟机,默认采用的DHCP的方式,但是随着虚拟机越来越多以后,这种方式很不方便,所以需要将机器的IP都设置为静态IP 需要添加的几项内容的解释:IPADDR IP ...
- JS倒计时代码
第一种:精确到秒的javascript倒计时代码 HTML代码: <form name="form1"> <div align="center" ...
- iOS开发Swift篇—简单介绍
iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...
- Qt resizeEvent 控件居中设置
在Qt中我们有时需要让一个控件在窗口居中或是在父控件中居中,而且在窗口拉伸时仍然保持正中央的位置.这里介绍一种方法,用到了主窗口的响应函数resizeEvent(QResizeEvent* event ...
- 安装rkhunter
#!/bin/bash if [ ! -d /soft ];thenmkdir /soft fiwhich rkhunterif [ $? -eq 0 ];then echo "rkhunt ...
- CUTE FTP 控制连接已关闭
使用Cute FTP连接FTP站点时,出现上述错误,在另外一台电脑上却可以正常连接. 原因:FTP服务器IP访问规则的限制 解决方法:在ServerU 服务器中进入服务器详细信息配置界面,在IP访问规 ...
- hdu-acm stepsHumble Numbers
这是我做的第六道动态规划水题,对动态规划差不多有了一个大致的概念.动态规划有几个关键因素,第一是最优子结构,第二是状态和状态转移方程.整个过程都是以 最优 为中心的.因此在状态转移方程中常涉及到几 ...
- JavaScript - prototype 和 call 的理解
prototype: 其实对象直接通过 [object.属性/方法] 来定义方法和属性也是可以的,但是会变成静态属性和静态方法,也就是调用的时候也是通过“.”来调用的.但如果通过prototype来 ...
- 【液晶模块系列基础视频】3.2fatfs接口函数的使用2
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...