Skip to content

Commit 736a96b

Browse files
committed
feat: add solutions to lc problem: No.1123
No.1123. Lowest Common Ancestor of Deepest Leaves
1 parent 217d4c6 commit 736a96b

File tree

4 files changed

+150
-24
lines changed

4 files changed

+150
-24
lines changed

solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README.md

+59-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:DFS**
65+
66+
我们设计一个函数 $dfs(root)$,它将返回一个二元组 $(l, d)$,其中 $l$ 是节点 $root$ 的最深公共祖先,而 $d$ 是节点 $root$ 的深度。函数 $dfs(root)$ 的执行逻辑如下:
67+
68+
- 如果 $root$ 为空,则返回二元组 $(None, 0)$;
69+
- 否则,我们递归调用 $dfs(root.left)$ 和 $dfs(root.right)$,得到二元组 $(l, d_1)$ 和 $(r, d_2)$。如果 $d_1 \gt d_2$,则 $root$ 的最深公共祖先节点为 $l$,深度为 $d_1 + 1$;如果 $d_1 \lt d_2$,则 $root$ 的最深公共祖先节点为 $r$,深度为 $d_2 + 1$;如果 $d_1 = d_2$,则 $root$ 的最深公共祖先节点为 $root$,深度为 $d_1 + 1$。
70+
71+
我们在主函数中调用 $dfs(root)$,并返回其返回值的第一个元素,即可得到最深公共祖先节点。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
74+
6475
<!-- tabs:start -->
6576

6677
### **Python3**
@@ -147,20 +158,24 @@ class Solution {
147158
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
148159
* };
149160
*/
150-
using pti = pair<TreeNode*, int>;
151161
class Solution {
152162
public:
153163
TreeNode* lcaDeepestLeaves(TreeNode* root) {
154164
return dfs(root).first;
155165
}
156166

157-
pti dfs(TreeNode* root) {
158-
if (!root) return {nullptr, 0};
159-
pti l = dfs(root->left);
160-
pti r = dfs(root->right);
161-
int d1 = l.second, d2 = r.second;
162-
if (d1 > d2) return {l.first, d1 + 1};
163-
if (d1 < d2) return {r.first, d2 + 1};
167+
pair<TreeNode*, int> dfs(TreeNode* root) {
168+
if (!root) {
169+
return {nullptr, 0};
170+
}
171+
auto [l, d1] = dfs(root->left);
172+
auto [r, d2] = dfs(root->right);
173+
if (d1 > d2) {
174+
return {l, d1 + 1};
175+
}
176+
if (d1 < d2) {
177+
return {r, d2 + 1};
178+
}
164179
return {root, d1 + 1};
165180
}
166181
};
@@ -202,6 +217,42 @@ func lcaDeepestLeaves(root *TreeNode) *TreeNode {
202217
}
203218
```
204219

220+
### **TypeScript**
221+
222+
```ts
223+
/**
224+
* Definition for a binary tree node.
225+
* class TreeNode {
226+
* val: number
227+
* left: TreeNode | null
228+
* right: TreeNode | null
229+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
230+
* this.val = (val===undefined ? 0 : val)
231+
* this.left = (left===undefined ? null : left)
232+
* this.right = (right===undefined ? null : right)
233+
* }
234+
* }
235+
*/
236+
237+
function lcaDeepestLeaves(root: TreeNode | null): TreeNode | null {
238+
const dfs = (root: TreeNode | null): [TreeNode | null, number] => {
239+
if (root === null) {
240+
return [null, 0];
241+
}
242+
const [l, d1] = dfs(root.left);
243+
const [r, d2] = dfs(root.right);
244+
if (d1 > d2) {
245+
return [l, d1 + 1];
246+
}
247+
if (d1 < d2) {
248+
return [r, d2 + 1];
249+
}
250+
return [root, d1 + 1];
251+
};
252+
return dfs(root)[0];
253+
}
254+
```
255+
205256
### **...**
206257

207258
```

solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README_EN.md

+48-8
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,24 @@ class Solution {
136136
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
137137
* };
138138
*/
139-
using pti = pair<TreeNode*, int>;
140139
class Solution {
141140
public:
142141
TreeNode* lcaDeepestLeaves(TreeNode* root) {
143142
return dfs(root).first;
144143
}
145144

146-
pti dfs(TreeNode* root) {
147-
if (!root) return {nullptr, 0};
148-
pti l = dfs(root->left);
149-
pti r = dfs(root->right);
150-
int d1 = l.second, d2 = r.second;
151-
if (d1 > d2) return {l.first, d1 + 1};
152-
if (d1 < d2) return {r.first, d2 + 1};
145+
pair<TreeNode*, int> dfs(TreeNode* root) {
146+
if (!root) {
147+
return {nullptr, 0};
148+
}
149+
auto [l, d1] = dfs(root->left);
150+
auto [r, d2] = dfs(root->right);
151+
if (d1 > d2) {
152+
return {l, d1 + 1};
153+
}
154+
if (d1 < d2) {
155+
return {r, d2 + 1};
156+
}
153157
return {root, d1 + 1};
154158
}
155159
};
@@ -191,6 +195,42 @@ func lcaDeepestLeaves(root *TreeNode) *TreeNode {
191195
}
192196
```
193197

198+
### **TypeScript**
199+
200+
```ts
201+
/**
202+
* Definition for a binary tree node.
203+
* class TreeNode {
204+
* val: number
205+
* left: TreeNode | null
206+
* right: TreeNode | null
207+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
208+
* this.val = (val===undefined ? 0 : val)
209+
* this.left = (left===undefined ? null : left)
210+
* this.right = (right===undefined ? null : right)
211+
* }
212+
* }
213+
*/
214+
215+
function lcaDeepestLeaves(root: TreeNode | null): TreeNode | null {
216+
const dfs = (root: TreeNode | null): [TreeNode | null, number] => {
217+
if (root === null) {
218+
return [null, 0];
219+
}
220+
const [l, d1] = dfs(root.left);
221+
const [r, d2] = dfs(root.right);
222+
if (d1 > d2) {
223+
return [l, d1 + 1];
224+
}
225+
if (d1 < d2) {
226+
return [r, d2 + 1];
227+
}
228+
return [root, d1 + 1];
229+
};
230+
return dfs(root)[0];
231+
}
232+
```
233+
194234
### **...**
195235

196236
```

solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@
99
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
1010
* };
1111
*/
12-
using pti = pair<TreeNode*, int>;
1312
class Solution {
1413
public:
1514
TreeNode* lcaDeepestLeaves(TreeNode* root) {
1615
return dfs(root).first;
1716
}
1817

19-
pti dfs(TreeNode* root) {
20-
if (!root) return {nullptr, 0};
21-
pti l = dfs(root->left);
22-
pti r = dfs(root->right);
23-
int d1 = l.second, d2 = r.second;
24-
if (d1 > d2) return {l.first, d1 + 1};
25-
if (d1 < d2) return {r.first, d2 + 1};
18+
pair<TreeNode*, int> dfs(TreeNode* root) {
19+
if (!root) {
20+
return {nullptr, 0};
21+
}
22+
auto [l, d1] = dfs(root->left);
23+
auto [r, d2] = dfs(root->right);
24+
if (d1 > d2) {
25+
return {l, d1 + 1};
26+
}
27+
if (d1 < d2) {
28+
return {r, d2 + 1};
29+
}
2630
return {root, d1 + 1};
2731
}
2832
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function lcaDeepestLeaves(root: TreeNode | null): TreeNode | null {
16+
const dfs = (root: TreeNode | null): [TreeNode | null, number] => {
17+
if (root === null) {
18+
return [null, 0];
19+
}
20+
const [l, d1] = dfs(root.left);
21+
const [r, d2] = dfs(root.right);
22+
if (d1 > d2) {
23+
return [l, d1 + 1];
24+
}
25+
if (d1 < d2) {
26+
return [r, d2 + 1];
27+
}
28+
return [root, d1 + 1];
29+
};
30+
return dfs(root)[0];
31+
}

0 commit comments

Comments
 (0)