Skip to content

Commit 94b6982

Browse files
committed
feat: add solutions to lc problem: No.1644
No.1644.Lowest Common Ancestor of a Binary Tree II
1 parent 6c100c5 commit 94b6982

File tree

6 files changed

+328
-2
lines changed

6 files changed

+328
-2
lines changed

solution/1600-1699/1644.Lowest Common Ancestor of a Binary Tree II/README.md

+112-1
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,133 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:递归(后序遍历)**
61+
62+
我们设计一个函数 $dfs(root, p, q)$,该函数返回以 $root$ 为根节点的二叉树中是否包含节点 $p$ 或节点 $q$,如果包含,则返回 `true`,否则返回 `false`
63+
64+
函数 $dfs(root, p, q)$ 的递归过程如下:
65+
66+
如果当前节点 $root$ 为空,则返回 `false`
67+
68+
否则,我们递归地遍历左子树和右子树,得到 $l$ 和 $r$,分别表示左子树和右子树中是否包含节点 $p$ 或节点 $q$。
69+
70+
如果 $l$ 和 $r$ 都为 `true`,说明当前节点 $root$ 就是我们要找的最近公共祖先节点,将其赋值给变量 $ans$。
71+
72+
如果 $l$ 或 $r$ 为 `true`,并且当前节点 $root$ 的值等于节点 $p$ 或节点 $q$ 的值,说明当前节点 $root$ 就是我们要找的最近公共祖先节点,将其赋值给变量 $ans$。
73+
74+
最后,我们判断 $l$ 或 $r$ 是否为 `true`,或者当前节点 $root$ 的值是否等于节点 $p$ 或节点 $q$ 的值,如果是,则返回 `true`,否则返回 `false`
75+
76+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
77+
6078
<!-- tabs:start -->
6179

6280
### **Python3**
6381

6482
<!-- 这里可写当前语言的特殊实现逻辑 -->
6583

6684
```python
67-
85+
# Definition for a binary tree node.
86+
# class TreeNode:
87+
# def __init__(self, x):
88+
# self.val = x
89+
# self.left = None
90+
# self.right = None
91+
92+
class Solution:
93+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
94+
def dfs(root, p, q):
95+
if root is None:
96+
return False
97+
l = dfs(root.left, p, q)
98+
r = dfs(root.right, p, q)
99+
nonlocal ans
100+
if l and r:
101+
ans = root
102+
if (l or r) and (root.val == p.val or root.val == q.val):
103+
ans = root
104+
return l or r or root.val == p.val or root.val == q.val
105+
106+
ans = None
107+
dfs(root, p, q)
108+
return ans
68109
```
69110

70111
### **Java**
71112

72113
<!-- 这里可写当前语言的特殊实现逻辑 -->
73114

74115
```java
116+
/**
117+
* Definition for a binary tree node.
118+
* public class TreeNode {
119+
* int val;
120+
* TreeNode left;
121+
* TreeNode right;
122+
* TreeNode(int x) { val = x; }
123+
* }
124+
*/
125+
class Solution {
126+
private TreeNode ans;
127+
128+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
129+
dfs(root, p, q);
130+
return ans;
131+
}
132+
133+
private boolean dfs(TreeNode root, TreeNode p, TreeNode q) {
134+
if (root == null) {
135+
return false;
136+
}
137+
boolean l = dfs(root.left, p, q);
138+
boolean r = dfs(root.right, p, q);
139+
if (l && r) {
140+
ans = root;
141+
}
142+
if ((l || r) && (root.val == p.val || root.val == q.val)) {
143+
ans = root;
144+
}
145+
return l || r || root.val == p.val || root.val == q.val;
146+
}
147+
}
148+
```
75149

150+
### **C++**
151+
152+
```cpp
153+
/**
154+
* Definition for a binary tree node.
155+
* struct TreeNode {
156+
* int val;
157+
* TreeNode *left;
158+
* TreeNode *right;
159+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
160+
* };
161+
*/
162+
class Solution {
163+
public:
164+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
165+
dfs(root, p, q);
166+
return ans;
167+
}
168+
169+
private:
170+
TreeNode* ans = nullptr;
171+
172+
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {
173+
if (!root) {
174+
return false;
175+
}
176+
bool l = dfs(root->left, p, q);
177+
bool r = dfs(root->right, p, q);
178+
if (l && r) {
179+
ans = root;
180+
}
181+
if ((l || r) && (root->val == p->val || root->val == q->val)) {
182+
ans = root;
183+
}
184+
return l || r || root->val == p->val || root->val == q->val;
185+
}
186+
};
76187
```
77188

78189
### **...**

solution/1600-1699/1644.Lowest Common Ancestor of a Binary Tree II/README_EN.md

+94-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,106 @@
5555
### **Python3**
5656

5757
```python
58-
58+
# Definition for a binary tree node.
59+
# class TreeNode:
60+
# def __init__(self, x):
61+
# self.val = x
62+
# self.left = None
63+
# self.right = None
64+
65+
class Solution:
66+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
67+
def dfs(root, p, q):
68+
if root is None:
69+
return False
70+
l = dfs(root.left, p, q)
71+
r = dfs(root.right, p, q)
72+
nonlocal ans
73+
if l and r:
74+
ans = root
75+
if (l or r) and (root.val == p.val or root.val == q.val):
76+
ans = root
77+
return l or r or root.val == p.val or root.val == q.val
78+
79+
ans = None
80+
dfs(root, p, q)
81+
return ans
5982
```
6083

6184
### **Java**
6285

6386
```java
87+
/**
88+
* Definition for a binary tree node.
89+
* public class TreeNode {
90+
* int val;
91+
* TreeNode left;
92+
* TreeNode right;
93+
* TreeNode(int x) { val = x; }
94+
* }
95+
*/
96+
class Solution {
97+
private TreeNode ans;
98+
99+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
100+
dfs(root, p, q);
101+
return ans;
102+
}
103+
104+
private boolean dfs(TreeNode root, TreeNode p, TreeNode q) {
105+
if (root == null) {
106+
return false;
107+
}
108+
boolean l = dfs(root.left, p, q);
109+
boolean r = dfs(root.right, p, q);
110+
if (l && r) {
111+
ans = root;
112+
}
113+
if ((l || r) && (root.val == p.val || root.val == q.val)) {
114+
ans = root;
115+
}
116+
return l || r || root.val == p.val || root.val == q.val;
117+
}
118+
}
119+
```
64120

121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for a binary tree node.
126+
* struct TreeNode {
127+
* int val;
128+
* TreeNode *left;
129+
* TreeNode *right;
130+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
131+
* };
132+
*/
133+
class Solution {
134+
public:
135+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
136+
dfs(root, p, q);
137+
return ans;
138+
}
139+
140+
private:
141+
TreeNode* ans = nullptr;
142+
143+
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {
144+
if (!root) {
145+
return false;
146+
}
147+
bool l = dfs(root->left, p, q);
148+
bool r = dfs(root->right, p, q);
149+
if (l && r) {
150+
ans = root;
151+
}
152+
if ((l || r) && (root->val == p->val || root->val == q->val)) {
153+
ans = root;
154+
}
155+
return l || r || root->val == p->val || root->val == q->val;
156+
}
157+
};
65158
```
66159

67160
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
13+
dfs(root, p, q);
14+
return ans;
15+
}
16+
17+
private:
18+
TreeNode* ans = nullptr;
19+
20+
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {
21+
if (!root) {
22+
return false;
23+
}
24+
bool l = dfs(root->left, p, q);
25+
bool r = dfs(root->right, p, q);
26+
if (l && r) {
27+
ans = root;
28+
}
29+
if ((l || r) && (root->val == p->val || root->val == q->val)) {
30+
ans = root;
31+
}
32+
return l || r || root->val == p->val || root->val == q->val;
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
private TreeNode ans;
12+
13+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
14+
dfs(root, p, q);
15+
return ans;
16+
}
17+
18+
private boolean dfs(TreeNode root, TreeNode p, TreeNode q) {
19+
if (root == null) {
20+
return false;
21+
}
22+
boolean l = dfs(root.left, p, q);
23+
boolean r = dfs(root.right, p, q);
24+
if (l && r) {
25+
ans = root;
26+
}
27+
if ((l || r) && (root.val == p.val || root.val == q.val)) {
28+
ans = root;
29+
}
30+
return l || r || root.val == p.val || root.val == q.val;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function (root, p, q) {
15+
const dfs = root => {
16+
if (!root) {
17+
return false;
18+
}
19+
const l = dfs(root.left);
20+
const r = dfs(root.right);
21+
if (l && r) {
22+
ans = root;
23+
}
24+
if ((l || r) && (root.val === p.val || root.val === q.val)) {
25+
ans = root;
26+
}
27+
return l || r || root.val === p.val || root.val === q.val;
28+
};
29+
let ans = null;
30+
dfs(root);
31+
return ans;
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
10+
def dfs(root, p, q):
11+
if root is None:
12+
return False
13+
l = dfs(root.left, p, q)
14+
r = dfs(root.right, p, q)
15+
nonlocal ans
16+
if l and r:
17+
ans = root
18+
if (l or r) and (root.val == p.val or root.val == q.val):
19+
ans = root
20+
return l or r or root.val == p.val or root.val == q.val
21+
22+
ans = None
23+
dfs(root, p, q)
24+
return ans

0 commit comments

Comments
 (0)