Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 49bb01d

Browse files
add LeetCode 236. 二叉树的最近公共祖先
1 parent 2041647 commit 49bb01d

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
6+
7+
百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(**一个节点也可以是它自己的祖先**)。"
8+
9+
例如,给定如下二叉树: `root = [3,5,1,6,2,0,8,null,null,7,4]`
10+
11+
![=](https://img-blog.csdnimg.cn/20200924184344131.png#pic_center)
12+
13+
14+
15+
16+
示例 1:
17+
18+
```javascript
19+
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
20+
输出: 3
21+
解释: 节点 5 和节点 1 的最近公共祖先是节点 3
22+
```
23+
24+
示例 2:
25+
26+
```javascript
27+
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
28+
输出: 5
29+
解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
30+
```
31+
32+
说明:
33+
34+
```javascript
35+
所有节点的值都是唯一的。
36+
p、q 为不同节点且均存在于给定的二叉树中。
37+
```
38+
39+
来源:力扣(LeetCode)
40+
链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree
41+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
42+
43+
## 解题思路
44+
根据定义,我们知道,假设 `root``p、q` 的最近公共祖先,则只可能有如下几种情况:
45+
46+
- p、q 在 root 的子树中,那么 p、q 分在 root 的左右子树中
47+
- 如果 p = root,那么 q 会在 p 的左右子树中,直接返回 p 即可
48+
- 如果 q = root,与上述类似
49+
50+
然后我们采用 `后序遍历`的方式,先遍历左右子树,看能不能找到对应 `p``q` 节点。
51+
52+
如果左右子树都能找到,那么代表p、q 分在 root 的左右子树中,直接返回 root 节点
53+
如果左子树找到,右子树没找到,那么就返回左子树的查找结果
54+
如果右子树找到,左子树没找到,那么就返回右子树的查找结果
55+
56+
![](https://img-blog.csdnimg.cn/20200924184419688.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
57+
58+
参考 <a href="https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/236-er-cha-shu-de-zui-jin-gong-gong-zu-xian-hou-xu/">Krahets</a> 大佬图解
59+
60+
61+
```javascript
62+
/**
63+
* Definition for a binary tree node.
64+
* function TreeNode(val) {
65+
* this.val = val;
66+
* this.left = this.right = null;
67+
* }
68+
*/
69+
/**
70+
* @param {TreeNode} root
71+
* @param {TreeNode} p
72+
* @param {TreeNode} q
73+
* @return {TreeNode}
74+
*/
75+
var lowestCommonAncestor = function (root, p, q) {
76+
if (!root || root == p || root == q) return root;
77+
let left = lowestCommonAncestor(root.left, p, q);
78+
let right = lowestCommonAncestor(root.right, p, q);
79+
// 如果当前root 在左右子树下可以找到 p q 那么这个root就是它们的最近公共祖先
80+
if(left && right) return root;
81+
// 如果当前root 只有左子树有节点,那么这个节点本身就是p、q的祖先
82+
else if(left) return left;
83+
// 如果当前root 只有右子树有节点,那么这个节点本身就是p、q的祖先
84+
else if(right) return right;
85+
// 如果左右子树都找不到p、q,那么这颗树不存在这两个点,直接返回 null 即可
86+
//(但本题告知已存在,所以下面这一行代码可写可不写都能通过)
87+
return null;
88+
};
89+
```
90+
91+
92+
## 最后
93+
文章产出不易,还望各位小伙伴们支持一波!
94+
95+
往期精选:
96+
97+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
98+
99+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
100+
101+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
102+
103+
104+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
105+
106+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
107+
108+
```javascript
109+
学如逆水行舟,不进则退
110+
```
111+
112+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /