41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
- “BFS 层次遍历”实现。
44
+ ** 方法一:BFS**
45
+
46
+ BFS 找每一层最大的节点值。
47
+
48
+ ** 方法二:DFS**
49
+
50
+ DFS 先序遍历,找每个深度最大的节点值。
45
51
46
52
<!-- tabs:start -->
47
53
57
63
# self.left = left
58
64
# self.right = right
59
65
class Solution :
60
- def largestValues (self , root : TreeNode) -> List[int ]:
66
+ def largestValues (self , root : Optional[ TreeNode] ) -> List[int ]:
61
67
if root is None :
62
68
return []
63
69
q = deque([root])
64
70
ans = []
65
71
while q:
66
- t = float ( ' -inf' )
72
+ t = - inf
67
73
for _ in range (len (q)):
68
74
node = q.popleft()
69
75
t = max (t, node.val)
@@ -75,6 +81,30 @@ class Solution:
75
81
return ans
76
82
```
77
83
84
+ ``` python
85
+ # Definition for a binary tree node.
86
+ # class TreeNode:
87
+ # def __init__(self, val=0, left=None, right=None):
88
+ # self.val = val
89
+ # self.left = left
90
+ # self.right = right
91
+ class Solution :
92
+ def largestValues (self , root : Optional[TreeNode]) -> List[int ]:
93
+ def dfs (root , curr ):
94
+ if root is None :
95
+ return
96
+ if curr == len (ans):
97
+ ans.append(root.val)
98
+ else :
99
+ ans[curr] = max (ans[curr], root.val)
100
+ dfs(root.left, curr + 1 )
101
+ dfs(root.right, curr + 1 )
102
+
103
+ ans = []
104
+ dfs(root, 0 )
105
+ return ans
106
+ ```
107
+
78
108
### ** Java**
79
109
80
110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -104,7 +134,7 @@ class Solution {
104
134
Deque<TreeNode > q = new ArrayDeque<> ();
105
135
q. offer(root);
106
136
while (! q. isEmpty()) {
107
- int t = Integer . MIN_VALUE ;
137
+ int t = q . peek() . val ;
108
138
for (int i = q. size(); i > 0 ; -- i) {
109
139
TreeNode node = q. poll();
110
140
t = Math . max(t, node. val);
@@ -122,6 +152,45 @@ class Solution {
122
152
}
123
153
```
124
154
155
+ ``` java
156
+ /**
157
+ * Definition for a binary tree node.
158
+ * public class TreeNode {
159
+ * int val;
160
+ * TreeNode left;
161
+ * TreeNode right;
162
+ * TreeNode() {}
163
+ * TreeNode(int val) { this.val = val; }
164
+ * TreeNode(int val, TreeNode left, TreeNode right) {
165
+ * this.val = val;
166
+ * this.left = left;
167
+ * this.right = right;
168
+ * }
169
+ * }
170
+ */
171
+ class Solution {
172
+ private List<Integer > ans = new ArrayList<> ();
173
+
174
+ public List<Integer > largestValues (TreeNode root ) {
175
+ dfs(root, 0 );
176
+ return ans;
177
+ }
178
+
179
+ private void dfs (TreeNode root , int curr ) {
180
+ if (root == null ) {
181
+ return ;
182
+ }
183
+ if (curr == ans. size()) {
184
+ ans. add(root. val);
185
+ } else {
186
+ ans. set(curr, Math . max(ans. get(curr), root. val));
187
+ }
188
+ dfs(root. left, curr + 1 );
189
+ dfs(root. right, curr + 1 );
190
+ }
191
+ }
192
+ ```
193
+
125
194
### ** C++**
126
195
127
196
``` cpp
@@ -144,12 +213,12 @@ public:
144
213
vector<int > ans;
145
214
while (!q.empty())
146
215
{
147
- int t = INT_MIN ;
148
- for (int i = q.size(); i > 0 ; --i)
216
+ int t = q.front()->val ;
217
+ for (int i = q.size(); i; --i)
149
218
{
150
- auto node = q.front();
151
- q.pop();
219
+ TreeNode* node = q.front();
152
220
t = max(t, node->val);
221
+ q.pop();
153
222
if (node->left) q.push(node->left);
154
223
if (node->right) q.push(node->right);
155
224
}
@@ -160,6 +229,37 @@ public:
160
229
};
161
230
```
162
231
232
+ ```cpp
233
+ /**
234
+ * Definition for a binary tree node.
235
+ * struct TreeNode {
236
+ * int val;
237
+ * TreeNode *left;
238
+ * TreeNode *right;
239
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
240
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
241
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
242
+ * };
243
+ */
244
+ class Solution {
245
+ public:
246
+ vector<int> ans;
247
+
248
+ vector<int> largestValues(TreeNode* root) {
249
+ dfs(root, 0);
250
+ return ans;
251
+ }
252
+
253
+ void dfs(TreeNode* root, int curr) {
254
+ if (!root) return;
255
+ if (curr == ans.size()) ans.push_back(root->val);
256
+ else ans[curr] = max(ans[curr], root->val);
257
+ dfs(root->left, curr + 1);
258
+ dfs(root->right, curr + 1);
259
+ }
260
+ };
261
+ ```
262
+
163
263
### ** Go**
164
264
165
265
``` go
@@ -176,9 +276,9 @@ func largestValues(root *TreeNode) []int {
176
276
if root == nil {
177
277
return ans
178
278
}
179
- var q = []*TreeNode{root}
279
+ q : = []*TreeNode{root}
180
280
for len (q) > 0 {
181
- t := math.MinInt32
281
+ t := q[ 0 ]. Val
182
282
for i := len (q); i > 0 ; i-- {
183
283
node := q[0 ]
184
284
q = q[1 :]
@@ -203,6 +303,42 @@ func max(a, b int) int {
203
303
}
204
304
```
205
305
306
+ ``` go
307
+ /* *
308
+ * Definition for a binary tree node.
309
+ * type TreeNode struct {
310
+ * Val int
311
+ * Left *TreeNode
312
+ * Right *TreeNode
313
+ * }
314
+ */
315
+ func largestValues (root *TreeNode ) []int {
316
+ var ans []int
317
+ var dfs func (*TreeNode, int )
318
+ dfs = func (root *TreeNode, curr int ) {
319
+ if root == nil {
320
+ return
321
+ }
322
+ if curr == len (ans) {
323
+ ans = append (ans, root.Val )
324
+ } else {
325
+ ans[curr] = max (ans[curr], root.Val )
326
+ }
327
+ dfs (root.Left , curr+1 )
328
+ dfs (root.Right , curr+1 )
329
+ }
330
+ dfs (root, 0 )
331
+ return ans
332
+ }
333
+
334
+ func max (a , b int ) int {
335
+ if a > b {
336
+ return a
337
+ }
338
+ return b
339
+ }
340
+ ```
341
+
206
342
### ** ...**
207
343
208
344
```
0 commit comments