[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 ...
随机推荐
- Alpha冲刺-第九次冲刺笔记
Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...
- 真香,理解记忆法学习Python基础语法
这篇文章很难写!我最开始学 Python,和大多数人一样,是看的菜鸟教程: 在写完这篇文章的第一遍后,我发现并没有写出新意,很可能读者看到后,会和我当初一样,很快就忘了.我现在已经不是读者而是作者了, ...
- JavaScript使用中的一些小技巧
任何一门技术在实际中都会有一些属于自己的小技巧.同样的,在使用JavaScript时也有一些自己的小技巧,只不过很多时候有可能容易被大家忽略.而在互联网上,时不时的有很多同行朋友会总结(或收集)一些这 ...
- Jmeter(三十一) - 从入门到精通 - Jmeter Http协议录制脚本工具-Badboy4(详解教程)
1.简介 上一篇文章中宏哥给小伙伴或童鞋们介绍讲解了手动添加Variable list的值,而实际工作中Badboy为我们提供了Variable setter工具,让我们不再使用哪一种比较笨拙的方法了 ...
- 转2:Python字符编码详解
1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...
- <阿里工程师的自我素养>读后感-技术人应该具备的一些基本素质
一.技术人具备"结构化思维"意味着什么? 1.什么是结构化思维? 结构化思维:逻辑+套路. 表达要有逻辑,所谓逻辑是指我们的结构之间必须是有逻辑关系的. 四种组织思想的逻辑关系 : ...
- sourcetree的使用(配合git)
主要讲解sourcetree的使用,是一个git提交的可视化软件,在官网上下载(windows,mac都有) 一路下载安装 首先,为了给本地sourcetree一个私钥,我们需要先下载git,然后在g ...
- java视频流的断点续传功能
项目中需要实现浏览器中视频的拖动问题解决 /** * 视频文件的断点续传功能 * @param path 文件路径 * @param request request * @param response ...
- LeetCode初级算法之字符串:242 有效的字母异位词
有效的字母异位词 题目地址:https://leetcode-cn.com/problems/valid-anagram/ 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位 ...
- 题解-洛谷P4859 已经没有什么好害怕的了
洛谷P4859 已经没有什么好害怕的了 给定 \(n\) 和 \(k\),\(n\) 个糖果能量 \(a_i\) 和 \(n\) 个药片能量 \(b_i\),每个 \(a_i\) 和 \(b_i\) ...