Skip to content

Commit cd178bd

Browse files
committed
feat: add solutions to lc problem: No.0236
No.0236.Lowest Common Ancestor of a Binary Tree
1 parent 802862a commit cd178bd

File tree

8 files changed

+169
-24
lines changed

8 files changed

+169
-24
lines changed

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md

+54-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:递归**
55+
5456
根据“**最近公共祖先**”的定义,若 root 是 p, q 的最近公共祖先 ,则只可能为以下情况之一:
5557

5658
- 如果 p 和 q 分别是 root 的左右节点,那么 root 就是我们要找的最近公共祖先;
@@ -84,11 +86,7 @@ class Solution:
8486
return root
8587
left = self.lowestCommonAncestor(root.left, p, q)
8688
right = self.lowestCommonAncestor(root.right, p, q)
87-
if left is None:
88-
return right
89-
if right is None:
90-
return left
91-
return root
89+
return root if left and right else (left or right)
9290
```
9391

9492
### **Java**
@@ -117,6 +115,57 @@ class Solution {
117115
}
118116
```
119117

118+
### **C++**
119+
120+
```cpp
121+
/**
122+
* Definition for a binary tree node.
123+
* struct TreeNode {
124+
* int val;
125+
* TreeNode *left;
126+
* TreeNode *right;
127+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
128+
* };
129+
*/
130+
class Solution {
131+
public:
132+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
133+
if (!root || root == p || root == q) return root;
134+
TreeNode* left = lowestCommonAncestor(root->left, p, q);
135+
TreeNode* right = lowestCommonAncestor(root->right, p, q);
136+
if (left && right) return root;
137+
return left ? left : right;
138+
}
139+
};
140+
```
141+
142+
### **Go**
143+
144+
```go
145+
/**
146+
* Definition for a binary tree node.
147+
* type TreeNode struct {
148+
* Val int
149+
* Left *TreeNode
150+
* Right *TreeNode
151+
* }
152+
*/
153+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
154+
if root == nil || root == p || root == q {
155+
return root
156+
}
157+
left := lowestCommonAncestor(root.Left, p, q)
158+
right := lowestCommonAncestor(root.Right, p, q)
159+
if left == nil {
160+
return right
161+
}
162+
if right == nil {
163+
return left
164+
}
165+
return root
166+
}
167+
```
168+
120169
### **JavaScript**
121170

122171
```js

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md

+52-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ class Solution:
6363
return root
6464
left = self.lowestCommonAncestor(root.left, p, q)
6565
right = self.lowestCommonAncestor(root.right, p, q)
66-
if left is None:
67-
return right
68-
if right is None:
69-
return left
70-
return root
66+
return root if left and right else (left or right)
7167
```
7268

7369
### **Java**
@@ -94,6 +90,57 @@ class Solution {
9490
}
9591
```
9692

93+
### **C++**
94+
95+
```cpp
96+
/**
97+
* Definition for a binary tree node.
98+
* struct TreeNode {
99+
* int val;
100+
* TreeNode *left;
101+
* TreeNode *right;
102+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
103+
* };
104+
*/
105+
class Solution {
106+
public:
107+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
108+
if (!root || root == p || root == q) return root;
109+
TreeNode* left = lowestCommonAncestor(root->left, p, q);
110+
TreeNode* right = lowestCommonAncestor(root->right, p, q);
111+
if (left && right) return root;
112+
return left ? left : right;
113+
}
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
/**
121+
* Definition for a binary tree node.
122+
* type TreeNode struct {
123+
* Val int
124+
* Left *TreeNode
125+
* Right *TreeNode
126+
* }
127+
*/
128+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
129+
if root == nil || root == p || root == q {
130+
return root
131+
}
132+
left := lowestCommonAncestor(root.Left, p, q)
133+
right := lowestCommonAncestor(root.Right, p, q)
134+
if left == nil {
135+
return right
136+
}
137+
if right == nil {
138+
return left
139+
}
140+
return root
141+
}
142+
```
143+
97144
### **JavaScript**
98145

99146
```js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
if (!root || root == p || root == q) return root;
14+
TreeNode* left = lowestCommonAncestor(root->left, p, q);
15+
TreeNode* right = lowestCommonAncestor(root->right, p, q);
16+
if (left && right) return root;
17+
return left ? left : right;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
10+
if root == nil || root == p || root == q {
11+
return root
12+
}
13+
left := lowestCommonAncestor(root.Left, p, q)
14+
right := lowestCommonAncestor(root.Right, p, q)
15+
if left == nil {
16+
return right
17+
}
18+
if right == nil {
19+
return left
20+
}
21+
return root
22+
}

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/Solution.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ def lowestCommonAncestor(
1414
return root
1515
left = self.lowestCommonAncestor(root.left, p, q)
1616
right = self.lowestCommonAncestor(root.right, p, q)
17-
if left is None:
18-
return right
19-
if right is None:
20-
return left
21-
return root
17+
return root if left and right else (left or right)

solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def minimumObstacles(self, grid: List[List[int]]) -> int:
1717
q.appendleft((x, y, k))
1818
else:
1919
q.append((x, y, k + 1))
20-
return 0
20+
return 0

solution/contest.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def handle(result: dict):
6060
'contest_title_slug': result['contest']['title_slug'],
6161
'contest_id': result['contest']['id'],
6262
'contest_start_time': result['contest']['origin_start_time'],
63-
'contest_duration': result['contest']['duration']
63+
'contest_duration': result['contest']['duration'],
6464
}
6565

6666

@@ -80,10 +80,16 @@ def run():
8080
biweekly_res.append(res)
8181
for v in chain(weekly_res, biweekly_res):
8282
handle(v)
83-
contest_list.append([
84-
v['contest']['id'], v['title'], v['title_en'], v['questions'], v['contest']['start_time'],
85-
v['contest']['duration']
86-
])
83+
contest_list.append(
84+
[
85+
v['contest']['id'],
86+
v['title'],
87+
v['title_en'],
88+
v['questions'],
89+
v['contest']['start_time'],
90+
v['contest']['duration'],
91+
]
92+
)
8793

8894
contest_list.sort(reverse=True)
8995
with open("contest.json", 'w', encoding='utf-8') as f:
@@ -94,6 +100,7 @@ def run():
94100

95101
# ["id", "title", "title_en", "questions", "start_time", "duration"]
96102

103+
97104
def generate_contest_list():
98105
with open('./result.json', 'r', encoding='utf-8') as f:
99106
result = json.loads(f.read())
@@ -112,7 +119,12 @@ def generate_contest_list():
112119
en_items = []
113120

114121
for cid, title, title_en, qs, start_time, duration in contest_list:
115-
v = "#### " + title + f'({format_time(start_time) + ", " + format_duration(duration)})' + "\n\n"
122+
v = (
123+
"#### "
124+
+ title
125+
+ f'({format_time(start_time) + ", " + format_duration(duration)})'
126+
+ "\n\n"
127+
)
116128
v_en = "#### " + title_en + "\n\n"
117129
for q in qs:
118130
slug = q['title_slug']

solution/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ def refresh(result):
152152
en_content = f2.read()
153153
i = cn_content.index('. ')
154154
j = cn_content.index(']')
155-
cn_content = cn_content.replace(cn_content[i + 2: j], title)
155+
cn_content = cn_content.replace(cn_content[i + 2 : j], title)
156156
i = en_content.index('. ')
157157
j = en_content.index(']')
158-
en_content = en_content.replace(en_content[i + 2: j], title_en)
158+
en_content = en_content.replace(en_content[i + 2 : j], title_en)
159159

160160
# update question content
161161
old_content = re.search("<!-- 这里写题目描述 -->(.*?)## 解法", cn_content, re.S).group(1)

0 commit comments

Comments
 (0)