MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / leftView

Method leftView

TreeLeftViewBinaryTree.java:4–32  ·  view source on GitHub ↗
(Node root)

Source from the content-addressed store, hash-verified

2{
3 //Function to return list containing elements of left view of binary tree.
4 ArrayList<Integer> leftView(Node root)
5 {
6 // Your code here
7 ArrayList<Integer> list = new ArrayList<>();
8 if(root==null) return list;
9 Queue<Node> queue = new LinkedList<>();
10 queue.add(root);
11 while(!queue.isEmpty())
12 {
13 int level = queue.size();
14 for(int i=0;i<level;i++)
15 {
16 if(queue.peek().left!=null)
17 {
18 queue.add(queue.peek().left);
19 }
20 if(queue.peek().right!=null)
21 {
22 queue.add(queue.peek().right);
23 }
24 if(i==0)
25 {
26 list.add(queue.peek().data);
27 }
28 queue.remove();
29 }
30 }
31 return list;
32 }
33}

Callers

nothing calls this directly

Calls 2

addMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected