Skip to content

Commit 105093d

Browse files
committed
feat: add solutions to lc problem: No.0865
No.0865.Smallest Subtree with all the Deepest Nodes
1 parent 21ee77a commit 105093d

File tree

6 files changed

+366
-25
lines changed

6 files changed

+366
-25
lines changed

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

+125-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<li>每个节点的值都是独一无二的。</li>
5858
</ul>
5959

60-
6160
## 解法
6261

6362
<!-- 这里可写通用的实现逻辑 -->
@@ -69,15 +68,139 @@
6968
<!-- 这里可写当前语言的特殊实现逻辑 -->
7069

7170
```python
72-
71+
# Definition for a binary tree node.
72+
# class TreeNode:
73+
# def __init__(self, val=0, left=None, right=None):
74+
# self.val = val
75+
# self.left = left
76+
# self.right = right
77+
class Solution:
78+
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
79+
def dfs(root):
80+
if not root:
81+
return None, 0
82+
l, d1 = dfs(root.left)
83+
r, d2 = dfs(root.right)
84+
if d1 > d2:
85+
return l, d1 + 1
86+
if d1 < d2:
87+
return r, d2 + 1
88+
return root, d1 + 1
89+
90+
return dfs(root)[0]
7391
```
7492

7593
### **Java**
7694

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

7997
```java
98+
/**
99+
* Definition for a binary tree node.
100+
* public class TreeNode {
101+
* int val;
102+
* TreeNode left;
103+
* TreeNode right;
104+
* TreeNode() {}
105+
* TreeNode(int val) { this.val = val; }
106+
* TreeNode(int val, TreeNode left, TreeNode right) {
107+
* this.val = val;
108+
* this.left = left;
109+
* this.right = right;
110+
* }
111+
* }
112+
*/
113+
class Solution {
114+
public TreeNode subtreeWithAllDeepest(TreeNode root) {
115+
return dfs(root)[0];
116+
}
117+
118+
private TreeNode[] dfs(TreeNode root) {
119+
if (root == null) {
120+
return new TreeNode[]{null, new TreeNode(0)};
121+
}
122+
TreeNode[] left = dfs(root.left);
123+
TreeNode[] right = dfs(root.right);
124+
int d1 = left[1].val, d2 = right[1].val;
125+
if (d1 > d2) {
126+
return new TreeNode[]{left[0], new TreeNode(d1 + 1)};
127+
}
128+
if (d1 < d2) {
129+
return new TreeNode[]{right[0], new TreeNode(d2 + 1)};
130+
}
131+
return new TreeNode[]{root, new TreeNode(d1 + 1)};
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
/**
140+
* Definition for a binary tree node.
141+
* struct TreeNode {
142+
* int val;
143+
* TreeNode *left;
144+
* TreeNode *right;
145+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
146+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
147+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
148+
* };
149+
*/
150+
class Solution {
151+
public:
152+
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
153+
return dfs(root).first;
154+
}
155+
156+
pair<TreeNode*, int> dfs(TreeNode* root) {
157+
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};
163+
return {root, d1 + 1};
164+
}
165+
};
166+
```
80167

168+
### **Go**
169+
170+
```go
171+
/**
172+
* Definition for a binary tree node.
173+
* type TreeNode struct {
174+
* Val int
175+
* Left *TreeNode
176+
* Right *TreeNode
177+
* }
178+
*/
179+
type Result struct {
180+
Node *TreeNode
181+
Depth int
182+
}
183+
184+
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,
192+
}
193+
}
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}
203+
}
81204
```
82205

83206
### **...**

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

+125-23
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@
66

77
<p>Given the <code>root</code> of a binary tree, the depth of each node is <strong>the shortest distance to the root</strong>.</p>
88

9-
10-
119
<p>Return <em>the smallest subtree</em> such that it contains <strong>all the deepest nodes</strong> in the original tree.</p>
1210

13-
14-
1511
<p>A node is called <strong>the&nbsp;deepest</strong> if it has the largest depth possible among&nbsp;any node in the entire tree.</p>
1612

17-
18-
1913
<p>The <strong>subtree</strong> of a node is tree consisting of that node, plus the set of all descendants of that node.</p>
2014

21-
22-
2315
<p><strong>Note:</strong> This question is the same as 1123: <a href="https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/" target="_blank">https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/</a></p>
2416

25-
26-
2717
<p>&nbsp;</p>
2818

2919
<p><strong>Example 1:</strong></p>
@@ -44,12 +34,8 @@ Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is
4434

4535
</pre>
4636

47-
48-
4937
<p><strong>Example 2:</strong></p>
5038

51-
52-
5339
<pre>
5440

5541
<strong>Input:</strong> root = [1]
@@ -60,12 +46,8 @@ Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is
6046

6147
</pre>
6248

63-
64-
6549
<p><strong>Example 3:</strong></p>
6650

67-
68-
6951
<pre>
7052

7153
<strong>Input:</strong> root = [0,1,3,null,2]
@@ -76,14 +58,10 @@ Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is
7658

7759
</pre>
7860

79-
80-
8161
<p>&nbsp;</p>
8262

8363
<p><strong>Constraints:</strong></p>
8464

85-
86-
8765
<ul>
8866
<li>The number of nodes in the tree will be in the range <code>[1, 500]</code>.</li>
8967
<li><code>0 &lt;= Node.val &lt;= 500</code></li>
@@ -97,13 +75,137 @@ Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is
9775
### **Python3**
9876

9977
```python
100-
78+
# Definition for a binary tree node.
79+
# class TreeNode:
80+
# def __init__(self, val=0, left=None, right=None):
81+
# self.val = val
82+
# self.left = left
83+
# self.right = right
84+
class Solution:
85+
def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:
86+
def dfs(root):
87+
if not root:
88+
return None, 0
89+
l, d1 = dfs(root.left)
90+
r, d2 = dfs(root.right)
91+
if d1 > d2:
92+
return l, d1 + 1
93+
if d1 < d2:
94+
return r, d2 + 1
95+
return root, d1 + 1
96+
97+
return dfs(root)[0]
10198
```
10299

103100
### **Java**
104101

105102
```java
103+
/**
104+
* Definition for a binary tree node.
105+
* public class TreeNode {
106+
* int val;
107+
* TreeNode left;
108+
* TreeNode right;
109+
* TreeNode() {}
110+
* TreeNode(int val) { this.val = val; }
111+
* TreeNode(int val, TreeNode left, TreeNode right) {
112+
* this.val = val;
113+
* this.left = left;
114+
* this.right = right;
115+
* }
116+
* }
117+
*/
118+
class Solution {
119+
public TreeNode subtreeWithAllDeepest(TreeNode root) {
120+
return dfs(root)[0];
121+
}
122+
123+
private TreeNode[] dfs(TreeNode root) {
124+
if (root == null) {
125+
return new TreeNode[]{null, new TreeNode(0)};
126+
}
127+
TreeNode[] left = dfs(root.left);
128+
TreeNode[] right = dfs(root.right);
129+
int d1 = left[1].val, d2 = right[1].val;
130+
if (d1 > d2) {
131+
return new TreeNode[]{left[0], new TreeNode(d1 + 1)};
132+
}
133+
if (d1 < d2) {
134+
return new TreeNode[]{right[0], new TreeNode(d2 + 1)};
135+
}
136+
return new TreeNode[]{root, new TreeNode(d1 + 1)};
137+
}
138+
}
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
/**
145+
* Definition for a binary tree node.
146+
* struct TreeNode {
147+
* int val;
148+
* TreeNode *left;
149+
* TreeNode *right;
150+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
151+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
152+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
153+
* };
154+
*/
155+
class Solution {
156+
public:
157+
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
158+
return dfs(root).first;
159+
}
160+
161+
pair<TreeNode*, int> dfs(TreeNode* root) {
162+
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};
168+
return {root, d1 + 1};
169+
}
170+
};
171+
```
106172

173+
### **Go**
174+
175+
```go
176+
/**
177+
* Definition for a binary tree node.
178+
* type TreeNode struct {
179+
* Val int
180+
* Left *TreeNode
181+
* Right *TreeNode
182+
* }
183+
*/
184+
type Result struct {
185+
Node *TreeNode
186+
Depth int
187+
}
188+
189+
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,
197+
}
198+
}
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}
208+
}
107209
```
108210

109211
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
15+
return dfs(root).first;
16+
}
17+
18+
pair<TreeNode*, int> dfs(TreeNode* root) {
19+
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};
25+
return {root, d1 + 1};
26+
}
27+
};

0 commit comments

Comments
 (0)