-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeftViewOfBinaryTree.java
46 lines (40 loc) · 1.17 KB
/
LeftViewOfBinaryTree.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
/************************************************************
Following is the TreeNode class structure
class TreeNode<T>
{
public:
T data;
TreeNode<T> left;
TreeNode<T> right;
TreeNode(T data)
{
this.data = data;
left = null;
right = null;
}
};
************************************************************/
import java.util.*;
public class Solution
{
public static ArrayList<Integer> getLeftView(TreeNode<Integer> root)
{
Queue<TreeNode<Integer>> queue = new LinkedList<>();
queue.offer(root);
ArrayList<Integer> result = new ArrayList<>();
if(root==null)
return result;
while(!queue.isEmpty()){
int size = queue.size();
result.add(queue.peek().data);
while(size-->0){
TreeNode<Integer> temp = queue.poll();
if(temp.left!=null)
queue.offer(temp.left);
if(temp.right!=null)
queue.offer(temp.right);
}
}
return result;
}
}