forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Largest Value in Each Tree Row.java
40 lines (37 loc) · 1.11 KB
/
Find Largest Value in Each Tree Row.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
// Runtime: 6 ms (Top 12.42%) | Memory: 45.3 MB (Top 32.64%)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> li=new ArrayList<>();
public List<Integer> largestValues(TreeNode root) {
if(root==null) return li; //if root is NULL
//using bfs(level-order)
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
int size=q.size();
int res=Integer.MIN_VALUE;
while(size-->0){
TreeNode temp=q.poll();
if(temp.left!=null) q.add(temp.left);
if(temp.right!=null) q.add(temp.right);
res =Math.max(res,temp.val); //comparing every node in each level to get max
}
li.add(res); //adding each level Max value to the list
}
return li;
}
}