-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintLeftViewIterative2
84 lines (72 loc) · 1.68 KB
/
PrintLeftViewIterative2
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Java program to print left view of binary tree //Using level order traversal
import java.util.Queue; //Time Complexity: O(n)
import java.util.LinkedList;
/* Class containing left and right child of current
node and key value*/
class Node
{
int data;
Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
/* Class to print the left view */
class BinaryTree
{
Node root;
static int max_level = 0;
// recursive function to print left view
void leftViewUtil(Node node)
{
// Base Case
if (node==null) return;
// Create an empty queue for level order tarversal
Queue<Node> q =new LinkedList<Node>();
// Enqueue Root and initialize height
q.add(root);
System.out.print(root.data + " ");
while(true)
{
// nodeCount (queue size) indicates number of nodes
// at current level.
int nodeCount = q.size();
if(nodeCount == 0)
break;
// Dequeue all nodes of current level and Enqueue all
// nodes of next level
while(nodeCount > 0)
{
node = q.peek();
q.remove();
if(node.left != null)
{
q.add(node.left);
System.out.print(node.left.data + " ");
}
if(node.right != null)
q.add(node.right);
nodeCount--;
}
}
}
// A wrapper over leftViewUtil()
void leftView()
{
leftViewUtil(root);
}
/* testing for example nodes */
public static void main(String args[])
{
/* creating a binary tree and entering the nodes */
BinaryTree tree = new BinaryTree();
tree.root = new Node(12);
tree.root.left = new Node(10);
tree.root.right = new Node(30);
tree.root.right.left = new Node(25);
tree.root.right.right = new Node(40);
tree.leftView();
}
}