236. Lowest Common Ancestor of a Binary 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.
更新于20180513
若pq都在某个节点的左边,就到左子树中查找,如果都在右边 就到右子树种查找。
要是pq不在同一边,那就表示已经找到第一个公共祖先。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(!cover(root,p)||!cover(root,q)) return null;
return LCAhelp(root,p,q);
}
private TreeNode LCAhelp(TreeNode root,TreeNode p,TreeNode q ){
if(root==null) return null;
if(root==p||root==q) return root;
boolean q_is_on_left = cover(root.left,q);
boolean p_is_on_left = cover(root.left,p); //分立两边
if(q_is_on_left!=p_is_on_left) return root; //在一边
else{
if(q_is_on_left)
return LCAhelp(root.left,p,q);
else
return LCAhelp(root.right,p,q);
}
}
private boolean cover(TreeNode root,TreeNode p){
//检查p是不是root的孙子 if(root==null) return false;
if(root==p) return true;
return cover(root.left,p)||cover(root.right,p);
}
}
解题思路
- Divide & Conquer 的思路
- 如果
root
为空,则返回空 - 如果
root
等于其中某个node
,则返回root
- 如果上述两种情况都不满足,则divide,左右子树分别调用该方法
- Divide & Conquer中治这一步要考虑清楚,本题三种情况
- 如果
left
和right
都有结果返回,说明root是最小公共祖先 - 如果只有
left
有返回值,说明left
的返回值是最小公共祖先 - 如果只有�
right
有返回值,说明�right
的返回值是最小公共祖先
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root ==null || p==root||q==root) return root;
TreeNode lp = lowestCommonAncestor(root.left,p,q);
TreeNode rp = lowestCommonAncestor(root.right,p,q);
if(lp!=null&&rp!=null) return root;
if(lp==null&&rp!=null) return rp;
if(lp!=null&&rp==null) return lp;
return null;
}
}
236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)的更多相关文章
- 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 Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 【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 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...
- 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
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- DpQuery.js
(function (window) { //添加事件的方法通用所有 function addevent(evetname, fn, obj) { if (document.attachEvent) ...
- 为 Ruby 程序员准备的 Go 入门教程
这是我翻译的国外博客,如需转载请注明出处和原文链接 那些在Google的大牛们开发出了一种称为Go的牛叉的语言.乍一看,Ruby和Go有点像远房表亲.其实不然,他们那些互为补充的功能却让他们成为一对完 ...
- python笔记2-数据类型:字符串常用操作
这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号.双引号或者三引号,如下: name = 'hello' name1 ...
- C# 路径的使用
// 摘要: // 获取或设置包含该应用程序的目录的名称. // // 返回结果: // 应用程序基目录的名称. AppDomain.CurrentDomain.SetupInformation.Ap ...
- WinCC7.3 Win764位系统安装教程
WinCC7.3 Win764位安装教程 (1)将ISO文件解压缩. (2)编辑Setup.ini文件 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...
- 【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库
1.mysql http://dev.mysql.com/doc/refman/5.5/en/tutorial.html mysql+python http://dev.mysql.com/doc/c ...
- 图像特征点匹配C代码
#include "opencv2/core/core.hpp" #include "highgui.h" #include "opencv2/img ...
- 把登录和退出功能单独写到一个公共.py脚本,其它用例test1,test2调用公共登录,退出函数
公共登录/退出函数模块(login_exit.py): #coding:utf-8import timedef login(driver, username, password):#此处的driver ...
- Android系统开发(3)——Makefile的编写
Makefile是什么? makefile的作用: 1.project文件组织,编译成复杂的程序 2.安装及卸载我们的程序 Makefile使用演示样例 在/home/username/makefil ...
- 笔试面试的路上——努力ing
积累: 最值得阅读学习的 10 个 C 语言开源项目代码 改变计算技术的 9 个伟大算法 程序员选修课丨追妹子的各种算法 追 MM 和设计模式:趣解 23 种设计模式 设计高可用性.容错和数据隔离的H ...