-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathLargetElementinRowsBT.java
51 lines (46 loc) · 1.36 KB
/
LargetElementinRowsBT.java
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
46
47
48
49
50
51
package problems.medium;
import problems.utils.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Created by sherxon on 2/12/17.
*/
/**
* Find Largest Element in Each Row of Binary Tree
*/
public class LargetElementinRowsBT {
public int[] largestValues(TreeNode root) {
return findValueMostElement(root);
}
/**
* I traversed level order and find max in each row .
*/
public int[] findValueMostElement(TreeNode root) {
if (root == null) return new int[0];
List<Integer> res = new ArrayList<>();
int level = 1;
LinkedList<TreeNode> list = new LinkedList<>();
list.add(root);
res.add(root.val);
while (!list.isEmpty()) {
TreeNode x = list.removeFirst();
level--;
if (x.left != null) list.addLast(x.left);
if (x.right != null) list.addLast(x.right);
if (level == 0 && !list.isEmpty()) {
int max = Integer.MIN_VALUE;
for (TreeNode treeNode : list) {
max = Math.max(treeNode.val, max);
level++;
}
res.add(max);
}
}
int[] a = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
a[i] = res.get(i);
}
return a;
}
}