-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindLargestValueInEachTreeRow.java
55 lines (53 loc) · 1.44 KB
/
FindLargestValueInEachTreeRow.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
52
53
54
55
/*https://leetcode.com/problems/find-largest-value-in-each-tree-row/*/
//level order traversal
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
TreeNode curr;
int len, i, max = Integer.MIN_VALUE;
queue.add(root);
while (!queue.isEmpty())
{
max = Integer.MIN_VALUE;
len = queue.size();
for (i = 0; i < len; ++i)
{
curr = queue.poll();
if (curr.val > max) max = curr.val;
if (curr.left != null) queue.add(curr.left);
if (curr.right != null) queue.add(curr.right);
}
result.add(max);
}
return result;
}
}
//recursion
class Solution {
ArrayList<Integer> res = new ArrayList<>();
int depth =0;
public List<Integer> largestValues(TreeNode root) {
traverse(root);
return res;
}
private void traverse(TreeNode root)
{
if(root==null)
return;
depth++;
if(depth>res.size())
{
res.add(root.val);
}
else
{
if(res.get(depth-1)<root.val)
res.set(depth-1,root.val);
}
traverse(root.left);
traverse(root.right);
depth--;
}
}