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 ...
随机推荐
- 利用多态,实现一般处理程序(ashx)中的AOP(切面编程)
本文是对工作中的项目进行代码优化(完善登陆验证的AOP切面编程)时,所遇到的各种解决方案思考过程. 项目背景:由ashx+nvelocity构建的简单B/S问卷系统,现需要优化登录验证环节(时隔若干个 ...
- Tensorflow之合并tensor
https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops.html#concat 例子: t1 = [[1, 2, 3], ...
- 出售 unity3d串口插件
出售unity3d串口插件 利用C++编写,解决了mono库 serialport的bug. serialport串口的bug地方在于: 1.有一些数据无法收到. 2.会丢失第一个字节. 3.延迟 我 ...
- Openstack(Kilo)安装系列之nova(七)
控制节点 Before you install and configure the Compute service, you must create a database, service crede ...
- Node.js 入门 资源
Node.js 入门 <快速搭建 Node.js 开发环境以及加速 npm> http://fengmk2.com/blog/2014/03/node-env-and-faster-npm ...
- GIT 回退出错 Unlink of file 'xx' failed. Should I try again? (y/n) 解决办法
发生过程 回退版本 如果回退版本时 里面有删除或者移动的文件 容易出这个问题 解决方法 git reset --hard 版本号 回退失败了 就 本地工作目录跟版本那个工作目录比较 然后还原修 ...
- Android无线测试之—UiAutomator UiObject API介绍四
输入文本与清除文本 一.输入文本与清除文本相关API 返回值 API 描述 boolean setText(String test) 在对象中输入文本 void clearTextField() 清除 ...
- Vue与React的异同 -生命周期
vue的生命周期 创建前 beforeCreate 创建 create 挂载前 beforeMount 挂载 mounted 更新前 beforeUpdate 更新 updated 销毁前 bef ...
- 常用sql 增删改、批量、合并、去重、增列、
自己总结的一些常用sql :插入.删除.批量更新.判重.新增列.数据库连接数 ---------------- 批量插入跨数据库 insert into ejpms.dbo.role (Name,In ...
- npm and node 的一些问题
1. node 安装 n 模块 用来管理 node的版本 2. 初始化项目出现这个问题 Error: Attempt to unlock XXX, which hasn't been locked ...