-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindLargestValueInEachTreeRow.java
More file actions
45 lines (33 loc) · 1.32 KB
/
Copy pathFindLargestValueInEachTreeRow.java
File metadata and controls
45 lines (33 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// You need to find the largest value in each row of a binary tree.
// See: https://leetcode.com/problems/find-largest-value-in-each-tree-row/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import leetcode.util.tree.TreeNode;
public class FindLargestValueInEachTreeRow {
public List<Integer> largestValues(TreeNode root) {
Map<Integer, Integer> maxMap = new LinkedHashMap<>();
dfs(root, 0, maxMap);
return new ArrayList<Integer>(maxMap.values());
}
private void dfs(TreeNode root, int depth, Map<Integer, Integer> maxMap) {
if (root == null) {
return;
}
if (root.val >= maxMap.getOrDefault(depth, Integer.MIN_VALUE)) {
maxMap.put(depth, root.val);
}
dfs(root.left, depth + 1, maxMap);
dfs(root.right, depth + 1, maxMap);
}
public static void main(String[] args) {
FindLargestValueInEachTreeRow sln = new FindLargestValueInEachTreeRow();
TreeNode t1 = initTree(1, 3, 2, 5, 3, null, 9);
System.out.println(sln.largestValues(t1));
System.out.println(sln.largestValues(null));
System.out.println(sln.largestValues(new TreeNode(-2147483648)));
}
}