Skip to content

Commit ed5fa35

Browse files
committed
leetcode: 1123
From: MacOS 一个暂未verify的commit 需要手动认证 Signed-off-by: LetMeFly666 <[email protected]>
1 parent 7e67f68 commit ed5fa35

7 files changed

+270
-17
lines changed

.commitmsg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
MGJW: 周报.(本周->上周)
1+
From: MacOS
2+
3+
一个暂未verify的commit
4+
需要手动认证
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-05 00:42:04
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-05 00:47:42
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
private:
24+
pair<TreeNode*, int> dfs(TreeNode* root) {
25+
if (!root) {
26+
return {root, 0};
27+
}
28+
pair<TreeNode*, int> left = dfs(root->left);
29+
pair<TreeNode*, int> right = dfs(root->right);
30+
if (left.second > right.second) {
31+
return {left.first, left.second + 1};
32+
} else if (left.second < right.second) {
33+
return {right.first, right.second + 1};
34+
}
35+
return {root, left.second + 1};
36+
}
37+
public:
38+
TreeNode* lcaDeepestLeaves(TreeNode* root) {
39+
return dfs(root).first;
40+
}
41+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-05 01:05:07
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-05 01:11:09
6+
*/
7+
package main
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* type TreeNode struct {
12+
* Val int
13+
* Left *TreeNode
14+
* Right *TreeNode
15+
* }
16+
*/
17+
type pair struct{
18+
node *TreeNode
19+
depth int
20+
}
21+
22+
func dfs(root* TreeNode) pair {
23+
if root == nil {
24+
return pair{root, 0}
25+
}
26+
left := dfs(root.Left)
27+
right := dfs(root.Right)
28+
if left.depth > right.depth {
29+
return pair{left.node, left.depth + 1}
30+
} else if left.depth < right.depth {
31+
return pair{right.node, right.depth + 1}
32+
}
33+
return pair{root, left.depth + 1}
34+
}
35+
36+
func lcaDeepestLeaves(root *TreeNode) *TreeNode {
37+
return dfs(root).node
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-05 00:59:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-05 01:03:59
6+
*/
7+
/**
8+
* Definition for a binary tree node.
9+
* public class TreeNode {
10+
* int val;
11+
* TreeNode left;
12+
* TreeNode right;
13+
* TreeNode() {}
14+
* TreeNode(int val) { this.val = val; }
15+
* TreeNode(int val, TreeNode left, TreeNode right) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
class Solution {
23+
private Pair<TreeNode, Integer> dfs(TreeNode root) {
24+
if (root == null) {
25+
return new Pair<>(root, 0);
26+
}
27+
Pair<TreeNode, Integer> left = dfs(root.left);
28+
Pair<TreeNode, Integer> right = dfs(root.right);
29+
if (left.getValue() > right.getValue()) {
30+
return new Pair<>(left.getKey(), left.getValue() + 1);
31+
} else if (left.getValue() < right.getValue()) {
32+
return new Pair<>(right.getKey(), right.getValue() + 1);
33+
}
34+
return new Pair<>(root, left.getValue() + 1);
35+
}
36+
37+
public TreeNode lcaDeepestLeaves(TreeNode root) {
38+
return dfs(root).getKey();
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-05 00:49:25
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-05 00:58:30
6+
'''
7+
from typing import Tuple, Optional
8+
9+
10+
# Definition for a binary tree node.
11+
class TreeNode:
12+
def __init__(self, val=0, left=None, right=None):
13+
self.val = val
14+
self.left = left
15+
self.right = right
16+
17+
class Solution:
18+
def dfs(self, root: Optional[TreeNode]) -> Tuple[Optional[TreeNode], int]:
19+
if not root:
20+
return (root, 0)
21+
left = self.dfs(root.left)
22+
right = self.dfs(root.right)
23+
if left[1] > right[1]:
24+
return (left[0], left[1] + 1)
25+
elif left[1] < right[1]:
26+
return (right[0], right[1] + 1)
27+
return (root, left[1] + 1)
28+
29+
def lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
30+
return self.dfs(root)[0]

Solutions/LeetCode 1123.最深叶节点的最近公共祖先.md

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ categories: [题解, LeetCode]
8080
#### C++
8181

8282
```cpp
83+
/*
84+
* @Author: LetMeFly
85+
* @Date: 2023-09-06 22:43:49
86+
* @LastEditors: LetMeFly
87+
* @LastEditTime: 2023-09-06 22:49:44
88+
*/
8389
typedef pair<TreeNode*, int> pti;
8490
class Solution {
8591
private:
@@ -109,31 +115,124 @@ public:
109115
#### Python
110116
111117
```python
112-
# from typing import Optional
113-
114-
# # Definition for a binary tree node.
115-
# class TreeNode:
116-
# def __init__(self, val=0, left=None, right=None):
117-
# self.val = val
118-
# self.left = left
119-
# self.right = right
118+
'''
119+
Author: LetMeFly
120+
Date: 2025-04-05 00:49:25
121+
LastEditors: LetMeFly.xyz
122+
LastEditTime: 2025-04-05 00:58:30
123+
'''
124+
from typing import Tuple, Optional
125+
126+
127+
# Definition for a binary tree node.
128+
class TreeNode:
129+
def __init__(self, val=0, left=None, right=None):
130+
self.val = val
131+
self.left = left
132+
self.right = right
120133
121134
class Solution:
122-
def dfs(self, root: Optional[TreeNode]):
135+
def dfs(self, root: Optional[TreeNode]) -> Tuple[Optional[TreeNode], int]:
123136
if not root:
124-
return [None, 0]
137+
return (root, 0)
125138
left = self.dfs(root.left)
126139
right = self.dfs(root.right)
127-
if left[1] == right[1]:
128-
return [root, left[1] + 1]
140+
if left[1] > right[1]:
141+
return (left[0], left[1] + 1)
129142
elif left[1] < right[1]:
130-
return [right[0], right[1] + 1]
131-
else:
132-
return [left[0], left[1] + 1]
133-
143+
return (right[0], right[1] + 1)
144+
return (root, left[1] + 1)
145+
134146
def lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
135147
return self.dfs(root)[0]
148+
```
149+
150+
#### Java
151+
152+
```java
153+
/*
154+
* @Author: LetMeFly
155+
* @Date: 2025-04-05 00:59:21
156+
* @LastEditors: LetMeFly.xyz
157+
* @LastEditTime: 2025-04-05 01:03:59
158+
*/
159+
/**
160+
* Definition for a binary tree node.
161+
* public class TreeNode {
162+
* int val;
163+
* TreeNode left;
164+
* TreeNode right;
165+
* TreeNode() {}
166+
* TreeNode(int val) { this.val = val; }
167+
* TreeNode(int val, TreeNode left, TreeNode right) {
168+
* this.val = val;
169+
* this.left = left;
170+
* this.right = right;
171+
* }
172+
* }
173+
*/
174+
class Solution {
175+
private Pair<TreeNode, Integer> dfs(TreeNode root) {
176+
if (root == null) {
177+
return new Pair<>(root, 0);
178+
}
179+
Pair<TreeNode, Integer> left = dfs(root.left);
180+
Pair<TreeNode, Integer> right = dfs(root.right);
181+
if (left.getValue() > right.getValue()) {
182+
return new Pair<>(left.getKey(), left.getValue() + 1);
183+
} else if (left.getValue() < right.getValue()) {
184+
return new Pair<>(right.getKey(), right.getValue() + 1);
185+
}
186+
return new Pair<>(root, left.getValue() + 1);
187+
}
188+
189+
public TreeNode lcaDeepestLeaves(TreeNode root) {
190+
return dfs(root).getKey();
191+
}
192+
}
193+
```
194+
195+
#### Go
196+
197+
```go
198+
/*
199+
* @Author: LetMeFly
200+
* @Date: 2025-04-05 01:05:07
201+
* @LastEditors: LetMeFly.xyz
202+
* @LastEditTime: 2025-04-05 01:11:09
203+
*/
204+
package main
205+
206+
/**
207+
* Definition for a binary tree node.
208+
* type TreeNode struct {
209+
* Val int
210+
* Left *TreeNode
211+
* Right *TreeNode
212+
* }
213+
*/
214+
type pair struct{
215+
node *TreeNode
216+
depth int
217+
}
218+
219+
func dfs(root* TreeNode) pair {
220+
if root == nil {
221+
return pair{root, 0}
222+
}
223+
left := dfs(root.Left)
224+
right := dfs(root.Right)
225+
if left.depth > right.depth {
226+
return pair{left.node, left.depth + 1}
227+
} else if left.depth < right.depth {
228+
return pair{right.node, right.depth + 1}
229+
}
230+
return pair{root, left.depth + 1}
231+
}
136232

233+
func lcaDeepestLeaves(root *TreeNode) *TreeNode {
234+
return dfs(root).node
235+
}
137236
```
138237

139238
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2023/09/06/LeetCode%201123.%E6%9C%80%E6%B7%B1%E5%8F%B6%E8%8A%82%E7%82%B9%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88/)哦~

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,8 @@ categories: [自用]
10811081
|||
10821082
|insofar|adv. 到如此程度<details><summary>例句</summary>I'll help you <font color="#28bea0">insofar</font> as I can.<br/>我将尽力帮助你。</details>|
10831083
|mitten|n. 连指手套|
1084+
|||
1085+
|luxurious|adj. 奢侈的,十分舒适的|
10841086

10851087
<p class="wordCounts">单词收录总数</p>
10861088

0 commit comments

Comments
 (0)