-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
FindLargestElementinEachRow.java
50 lines (45 loc) · 1.32 KB
/
FindLargestElementinEachRow.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
package problems.medium;
import problems.utils.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by sherxon on 2/11/17.
*/
/**
*
* */
public class FindLargestElementinEachRow {
public static void main(String[] args) {
List<Integer> list= new ArrayList<>();
String a=list.stream().map(String::valueOf).collect(Collectors.joining(","));
}
public List<Integer> findValueMostElement(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null)return res;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
res.add(root.val);
int level=1;
int max = Integer.MIN_VALUE;
while (!queue.isEmpty()) {
TreeNode x = queue.removeFirst();
level--;
if (x.left != null) {
queue.addLast(x.left);
max=Math.max(max, x.left.val);
}
if (x.right != null){
queue.addLast(x.right);
max=Math.max(max, x.right.val);
}
if (level==0 && !queue.isEmpty()) {
res.add(max);
max = Integer.MIN_VALUE;
level+=queue.size();
}
}
return res;
}
}