Skip to content

Commit a28fec1

Browse files
committed
feat: add solutions to lc problem: No.1123
No.1123.Lowest Common Ancestor of Deepest Leaves
1 parent 3b80455 commit a28fec1

File tree

12 files changed

+458
-127
lines changed

12 files changed

+458
-127
lines changed

solution/0800-0899/0865.Smallest Subtree with all the Deepest Nodes/README.md

+34-35
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
class Solution:
7878
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
7979
def dfs(root):
80-
if not root:
80+
if root is None:
8181
return None, 0
8282
l, d1 = dfs(root.left)
8383
r, d2 = dfs(root.right)
@@ -112,23 +112,23 @@ class Solution:
112112
*/
113113
class Solution {
114114
public TreeNode subtreeWithAllDeepest(TreeNode root) {
115-
return dfs(root)[0];
115+
return dfs(root).getKey();
116116
}
117117

118-
private TreeNode[] dfs(TreeNode root) {
118+
private Pair<TreeNode, Integer> dfs(TreeNode root) {
119119
if (root == null) {
120-
return new TreeNode[]{null, new TreeNode(0)};
120+
return new Pair<>(null, 0);
121121
}
122-
TreeNode[] left = dfs(root.left);
123-
TreeNode[] right = dfs(root.right);
124-
int d1 = left[1].val, d2 = right[1].val;
122+
Pair<TreeNode, Integer> l = dfs(root.left);
123+
Pair<TreeNode, Integer> r = dfs(root.right);
124+
int d1 = l.getValue(), d2 = r.getValue();
125125
if (d1 > d2) {
126-
return new TreeNode[]{left[0], new TreeNode(d1 + 1)};
126+
return new Pair<>(l.getKey(), d1 + 1);
127127
}
128128
if (d1 < d2) {
129-
return new TreeNode[]{right[0], new TreeNode(d2 + 1)};
129+
return new Pair<>(r.getKey(), d2 + 1);
130130
}
131-
return new TreeNode[]{root, new TreeNode(d1 + 1)};
131+
return new Pair<>(root, d1 + 1);
132132
}
133133
}
134134
```
@@ -147,19 +147,20 @@ class Solution {
147147
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
148148
* };
149149
*/
150+
using pti = pair<TreeNode*, int>;
150151
class Solution {
151152
public:
152153
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
153154
return dfs(root).first;
154155
}
155156

156-
pair<TreeNode*, int> dfs(TreeNode* root) {
157+
pti dfs(TreeNode* root) {
157158
if (!root) return {nullptr, 0};
158-
auto left = dfs(root->left);
159-
auto right = dfs(root->right);
160-
int d1 = left.second, d2 = right.second;
161-
if (d1 > d2) return {left.first, d1 + 1};
162-
if (d1 < d2) return {right.first, d2 + 1};
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};
163164
return {root, d1 + 1};
164165
}
165166
};
@@ -176,30 +177,28 @@ public:
176177
* Right *TreeNode
177178
* }
178179
*/
179-
type Result struct {
180-
Node *TreeNode
181-
Depth int
180+
type pair struct {
181+
first *TreeNode
182+
second int
182183
}
183184

184185
func subtreeWithAllDeepest(root *TreeNode) *TreeNode {
185-
return dfs(root).Node
186-
}
187-
188-
func dfs(root *TreeNode) Result {
189-
if root == nil {
190-
return Result{
191-
nil, 0,
186+
var dfs func(root *TreeNode) pair
187+
dfs = func(root *TreeNode) pair {
188+
if root == nil {
189+
return pair{nil, 0}
192190
}
191+
l, r := dfs(root.Left), dfs(root.Right)
192+
d1, d2 := l.second, r.second
193+
if d1 > d2 {
194+
return pair{l.first, d1 + 1}
195+
}
196+
if d1 < d2 {
197+
return pair{r.first, d2 + 1}
198+
}
199+
return pair{root, d1 + 1}
193200
}
194-
left, right := dfs(root.Left), dfs(root.Right)
195-
d1, d2 := left.Depth, right.Depth
196-
if d1 > d2 {
197-
return Result{left.Node, d1 + 1}
198-
}
199-
if d1 < d2 {
200-
return Result{right.Node, d2 + 1}
201-
}
202-
return Result{root, d1 + 1}
201+
return dfs(root).first
203202
}
204203
```
205204

solution/0800-0899/0865.Smallest Subtree with all the Deepest Nodes/README_EN.md

+34-35
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is
8484
class Solution:
8585
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
8686
def dfs(root):
87-
if not root:
87+
if root is None:
8888
return None, 0
8989
l, d1 = dfs(root.left)
9090
r, d2 = dfs(root.right)
@@ -117,23 +117,23 @@ class Solution:
117117
*/
118118
class Solution {
119119
public TreeNode subtreeWithAllDeepest(TreeNode root) {
120-
return dfs(root)[0];
120+
return dfs(root).getKey();
121121
}
122122

123-
private TreeNode[] dfs(TreeNode root) {
123+
private Pair<TreeNode, Integer> dfs(TreeNode root) {
124124
if (root == null) {
125-
return new TreeNode[]{null, new TreeNode(0)};
125+
return new Pair<>(null, 0);
126126
}
127-
TreeNode[] left = dfs(root.left);
128-
TreeNode[] right = dfs(root.right);
129-
int d1 = left[1].val, d2 = right[1].val;
127+
Pair<TreeNode, Integer> l = dfs(root.left);
128+
Pair<TreeNode, Integer> r = dfs(root.right);
129+
int d1 = l.getValue(), d2 = r.getValue();
130130
if (d1 > d2) {
131-
return new TreeNode[]{left[0], new TreeNode(d1 + 1)};
131+
return new Pair<>(l.getKey(), d1 + 1);
132132
}
133133
if (d1 < d2) {
134-
return new TreeNode[]{right[0], new TreeNode(d2 + 1)};
134+
return new Pair<>(r.getKey(), d2 + 1);
135135
}
136-
return new TreeNode[]{root, new TreeNode(d1 + 1)};
136+
return new Pair<>(root, d1 + 1);
137137
}
138138
}
139139
```
@@ -152,19 +152,20 @@ class Solution {
152152
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
153153
* };
154154
*/
155+
using pti = pair<TreeNode*, int>;
155156
class Solution {
156157
public:
157158
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
158159
return dfs(root).first;
159160
}
160161

161-
pair<TreeNode*, int> dfs(TreeNode* root) {
162+
pti dfs(TreeNode* root) {
162163
if (!root) return {nullptr, 0};
163-
auto left = dfs(root->left);
164-
auto right = dfs(root->right);
165-
int d1 = left.second, d2 = right.second;
166-
if (d1 > d2) return {left.first, d1 + 1};
167-
if (d1 < d2) return {right.first, d2 + 1};
164+
pti l = dfs(root->left);
165+
pti r = dfs(root->right);
166+
int d1 = l.second, d2 = r.second;
167+
if (d1 > d2) return {l.first, d1 + 1};
168+
if (d1 < d2) return {r.first, d2 + 1};
168169
return {root, d1 + 1};
169170
}
170171
};
@@ -181,30 +182,28 @@ public:
181182
* Right *TreeNode
182183
* }
183184
*/
184-
type Result struct {
185-
Node *TreeNode
186-
Depth int
185+
type pair struct {
186+
first *TreeNode
187+
second int
187188
}
188189

189190
func subtreeWithAllDeepest(root *TreeNode) *TreeNode {
190-
return dfs(root).Node
191-
}
192-
193-
func dfs(root *TreeNode) Result {
194-
if root == nil {
195-
return Result{
196-
nil, 0,
191+
var dfs func(root *TreeNode) pair
192+
dfs = func(root *TreeNode) pair {
193+
if root == nil {
194+
return pair{nil, 0}
197195
}
196+
l, r := dfs(root.Left), dfs(root.Right)
197+
d1, d2 := l.second, r.second
198+
if d1 > d2 {
199+
return pair{l.first, d1 + 1}
200+
}
201+
if d1 < d2 {
202+
return pair{r.first, d2 + 1}
203+
}
204+
return pair{root, d1 + 1}
198205
}
199-
left, right := dfs(root.Left), dfs(root.Right)
200-
d1, d2 := left.Depth, right.Depth
201-
if d1 > d2 {
202-
return Result{left.Node, d1 + 1}
203-
}
204-
if d1 < d2 {
205-
return Result{right.Node, d2 + 1}
206-
}
207-
return Result{root, d1 + 1}
206+
return dfs(root).first
208207
}
209208
```
210209

solution/0800-0899/0865.Smallest Subtree with all the Deepest Nodes/Solution.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
1010
* };
1111
*/
12+
using pti = pair<TreeNode*, int>;
1213
class Solution {
1314
public:
1415
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
1516
return dfs(root).first;
1617
}
1718

18-
pair<TreeNode*, int> dfs(TreeNode* root) {
19+
pti dfs(TreeNode* root) {
1920
if (!root) return {nullptr, 0};
20-
auto left = dfs(root->left);
21-
auto right = dfs(root->right);
22-
int d1 = left.second, d2 = right.second;
23-
if (d1 > d2) return {left.first, d1 + 1};
24-
if (d1 < d2) return {right.first, d2 + 1};
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};
2526
return {root, d1 + 1};
2627
}
2728
};

solution/0800-0899/0865.Smallest Subtree with all the Deepest Nodes/Solution.go

+17-19
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,26 @@
66
* Right *TreeNode
77
* }
88
*/
9-
type Result struct {
10-
Node *TreeNode
11-
Depth int
9+
type pair struct {
10+
first *TreeNode
11+
second int
1212
}
1313

1414
func subtreeWithAllDeepest(root *TreeNode) *TreeNode {
15-
return dfs(root).Node
16-
}
17-
18-
func dfs(root *TreeNode) Result {
19-
if root == nil {
20-
return Result{
21-
nil, 0,
15+
var dfs func(root *TreeNode) pair
16+
dfs = func(root *TreeNode) pair {
17+
if root == nil {
18+
return pair{nil, 0}
2219
}
20+
l, r := dfs(root.Left), dfs(root.Right)
21+
d1, d2 := l.second, r.second
22+
if d1 > d2 {
23+
return pair{l.first, d1 + 1}
24+
}
25+
if d1 < d2 {
26+
return pair{r.first, d2 + 1}
27+
}
28+
return pair{root, d1 + 1}
2329
}
24-
left, right := dfs(root.Left), dfs(root.Right)
25-
d1, d2 := left.Depth, right.Depth
26-
if d1 > d2 {
27-
return Result{left.Node, d1 + 1}
28-
}
29-
if d1 < d2 {
30-
return Result{right.Node, d2 + 1}
31-
}
32-
return Result{root, d1 + 1}
30+
return dfs(root).first
3331
}

solution/0800-0899/0865.Smallest Subtree with all the Deepest Nodes/Solution.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
*/
1616
class Solution {
1717
public TreeNode subtreeWithAllDeepest(TreeNode root) {
18-
return dfs(root)[0];
18+
return dfs(root).getKey();
1919
}
20-
21-
private TreeNode[] dfs(TreeNode root) {
20+
21+
private Pair<TreeNode, Integer> dfs(TreeNode root) {
2222
if (root == null) {
23-
return new TreeNode[]{null, new TreeNode(0)};
23+
return new Pair<>(null, 0);
2424
}
25-
TreeNode[] left = dfs(root.left);
26-
TreeNode[] right = dfs(root.right);
27-
int d1 = left[1].val, d2 = right[1].val;
25+
Pair<TreeNode, Integer> l = dfs(root.left);
26+
Pair<TreeNode, Integer> r = dfs(root.right);
27+
int d1 = l.getValue(), d2 = r.getValue();
2828
if (d1 > d2) {
29-
return new TreeNode[]{left[0], new TreeNode(d1 + 1)};
29+
return new Pair<>(l.getKey(), d1 + 1);
3030
}
3131
if (d1 < d2) {
32-
return new TreeNode[]{right[0], new TreeNode(d2 + 1)};
32+
return new Pair<>(r.getKey(), d2 + 1);
3333
}
34-
return new TreeNode[]{root, new TreeNode(d1 + 1)};
34+
return new Pair<>(root, d1 + 1);
3535
}
3636
}

solution/0800-0899/0865.Smallest Subtree with all the Deepest Nodes/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Solution:
88
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
99
def dfs(root):
10-
if not root:
10+
if root is None:
1111
return None, 0
1212
l, d1 = dfs(root.left)
1313
r, d2 = dfs(root.right)

0 commit comments

Comments
 (0)