-
Notifications
You must be signed in to change notification settings - Fork 17
/
PrintLeftView.java
76 lines (71 loc) · 2.12 KB
/
PrintLeftView.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package SummerTrainingGFG.Tree;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Vishal Singh
*/
public class PrintLeftView {
static class Node{
int key;
Node left,right;
Node(int key){
this.key = key;
}
}
static int maxLevel = 0;
/*Recursive*/
static void printLeftViewRecursive(Node root, int level){
if (root == null){
return;
}
if (maxLevel < level){
System.out.print(root.key+" ");
maxLevel = level;
}
else {
printLeftViewRecursive(root.left,level+1);
printLeftViewRecursive(root.left,level+1);
}
}
/*Iterative*/
static void printLeftViewIterative(Node root){
if (root == null)
return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()){
int size = q.size();
for (int i = 0; i < size; i++) {
Node curr = q.poll();
if (i == 0){
System.out.print(curr.key+" ");
}
if (curr.left != null){
q.add(curr.left);
}
if (curr.right != null){
q.add(curr.right);
}
}
}
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(20);
root.left.left = new Node(60);
root.left.left.left = new Node(70);
root.left.left.left.left = new Node(90);
root.left.left.left.right = new Node(80);
root.left.left.left.right.left = new Node(100);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.left.left = new Node(500);
root.right.left.left.left = new Node(180);
root.right.left.left.left.left = new Node(280);
root.right.left.left.left.left.right = new Node(500);
root.right.right = new Node(50);
root.right.right.right = new Node(900);
printLeftViewRecursive(root, 1);
printLeftViewIterative(root);
}
}