Skip to content

Commit 4b0b7f1

Browse files
committed
feat: add solutions to lc problem: No.0515
No.0515.Find Largest Value in Each Tree Row
1 parent f980593 commit 4b0b7f1

File tree

6 files changed

+294
-28
lines changed

6 files changed

+294
-28
lines changed

solution/0500-0599/0515.Find Largest Value in Each Tree Row/README.md

Lines changed: 146 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
“BFS 层次遍历”实现。
44+
**方法一:BFS**
45+
46+
BFS 找每一层最大的节点值。
47+
48+
**方法二:DFS**
49+
50+
DFS 先序遍历,找每个深度最大的节点值。
4551

4652
<!-- tabs:start -->
4753

@@ -57,13 +63,13 @@
5763
# self.left = left
5864
# self.right = right
5965
class Solution:
60-
def largestValues(self, root: TreeNode) -> List[int]:
66+
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
6167
if root is None:
6268
return []
6369
q = deque([root])
6470
ans = []
6571
while q:
66-
t = float('-inf')
72+
t = -inf
6773
for _ in range(len(q)):
6874
node = q.popleft()
6975
t = max(t, node.val)
@@ -75,6 +81,30 @@ class Solution:
7581
return ans
7682
```
7783

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+
78108
### **Java**
79109

80110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -104,7 +134,7 @@ class Solution {
104134
Deque<TreeNode> q = new ArrayDeque<>();
105135
q.offer(root);
106136
while (!q.isEmpty()) {
107-
int t = Integer.MIN_VALUE;
137+
int t = q.peek().val;
108138
for (int i = q.size(); i > 0; --i) {
109139
TreeNode node = q.poll();
110140
t = Math.max(t, node.val);
@@ -122,6 +152,45 @@ class Solution {
122152
}
123153
```
124154

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+
125194
### **C++**
126195

127196
```cpp
@@ -144,12 +213,12 @@ public:
144213
vector<int> ans;
145214
while (!q.empty())
146215
{
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)
149218
{
150-
auto node = q.front();
151-
q.pop();
219+
TreeNode* node = q.front();
152220
t = max(t, node->val);
221+
q.pop();
153222
if (node->left) q.push(node->left);
154223
if (node->right) q.push(node->right);
155224
}
@@ -160,6 +229,37 @@ public:
160229
};
161230
```
162231
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+
163263
### **Go**
164264

165265
```go
@@ -176,9 +276,9 @@ func largestValues(root *TreeNode) []int {
176276
if root == nil {
177277
return ans
178278
}
179-
var q = []*TreeNode{root}
279+
q := []*TreeNode{root}
180280
for len(q) > 0 {
181-
t := math.MinInt32
281+
t := q[0].Val
182282
for i := len(q); i > 0; i-- {
183283
node := q[0]
184284
q = q[1:]
@@ -203,6 +303,42 @@ func max(a, b int) int {
203303
}
204304
```
205305

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+
206342
### **...**
207343

208344
```

0 commit comments

Comments
 (0)