[leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的。
如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能是一个节点的祖先,也可能是两个节点的祖先,如果是一个节点的祖先,那么公共祖先还在上边,还需要返回结果进行判断,如果是两个节点的祖先,那么最小公共祖先就是这个或者在下边,总之,返回有结果的那个子树就是了。
所以思路就是,往下遍历,直到找到节点然后返回,返回以后判断此时左右子树的情况,根据情况返回根节点的递归结果或者子树的递归结果
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
/*
从底下开始判断,两个节点是不是在左右子树上
*/
if(root==null) return null;
//如果遍历到了某个节点,说明这个节点在这个子树上,返回这个节点
if (root==p||root==q) return root;
//如果还没遍历到,那么公共节点肯定在下边,继续往下寻找左子树和右子树
//返回的两个分别是左、右子树上遍历到的某个节点的祖先
TreeNode lt = lowestCommonAncestor(root.left,p,q);
TreeNode rt = lowestCommonAncestor(root.right,p,q);
//如果左右子树都找到了,那么这个节点就是公共祖先
if (lt!=null&&rt!=null) return root;
//如果有空缺,那么返回那个找到的,这种情况是到现在为止,两个节点已经在一棵子树上了,返回以当前子树为起点的运算结果就行
return (lt==null)?rt:lt;
}
[leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先的更多相关文章
- [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 ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- 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 ...
- (medium)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. Accordi ...
- [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 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
随机推荐
- 在 Windows 中使用 C# 启动其他程序
因为某些原因需要自动启动一个 Winform 程序,可能是因为第三方资源的原因,使用 System.Diagnostics.Process 无法成功启动 (可以看到界面,但是会报 Unhandled ...
- 泓格WINPAC主机与第三方模块rs 485 modbus rtu通信测试
开发语言:C# 开发环境:VS2008(支持WINCE开发的最后一个版本) 运行环境:Windows CE 5.0~7.0 项目说明:多台涨格winpac系列的主机,原来使用泓格SDK开发的程序,采集 ...
- django项目初始化
1.为了方便管理app,我们添加专门的apps文件夹来存放所有的app.结构如下 1.1设置完apps文件夹以后我们需要对配置文件做相应的更改 1.1.1.在seetings.py里添加django文 ...
- redis雪崩,击穿,穿透
redis穿透 什么是redis穿透? 1.查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存 2.这将导致这个不存在的数据每次请求都要到存储层 ...
- PyQt(Python+Qt)学习随笔:QTableWidget设置项的setItem方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget表格部件中setItem用于在表格部件QTableWidget创建后,设定指 ...
- BUUOJ 杂项MISC(1)
爱因斯坦 下载之后解压打开是一张爱因斯坦的图片,看来是图片隐写题 使用binwalk -e misc2.jpg 获得一个有flag.txt的压缩包,但是需要密码才能打开,猜想密码在图片里面,把图片丢进 ...
- ios移动端 clipboard点击复制失效
在使用clipboard.min.js插件库实现复制,android下没有问题,ios下无效! 原因:ios默认非点击标签没有点击效果 解决方法:需要给非点击标签加事件,比如在span,div或者p标 ...
- Python迭代器&生成器&装饰器
1. 迭代器 1.1 可迭代对象(Iterator) 迭代器协议:某对象必须提供一个__next__()方法,执行方法要么返回迭代中的下一项,要么引起一个Stopiteration异常,以终止迭代(只 ...
- Fabric v2.0中的隐私数据
文章来源于https://hyperledger-fabric.readthedocs.io/en/release-2.0/ 私有数据集在v1.4中提出,一直使用的是隐私数据集方式,即建立一个隐私数据 ...
- Angular:惰性加载的模块
①通过ng new angular-module创建一个全新的angular应用,默认不选路由 ②通过一下命令分别创建2个模块和1个组件 ng g m hx1 ng g c hx1 ng g m hx ...