-
Notifications
You must be signed in to change notification settings - Fork 13
/
Tree_LeftViewOfBinaryTree.java
136 lines (109 loc) · 3.66 KB
/
Tree_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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
// https://practice.geeksforgeeks.org/problems/left-view-of-binary-tree/1
// https://www.geeksforgeeks.org/print-left-view-binary-tree/
// https://practice.geeksforgeeks.org/problems/right-view-of-binary-tree/1 (Similar question)
// https://leetcode.com/problems/binary-tree-right-side-view/ (Similar question)
public class Tree_LeftViewOfBinaryTree {
/* same logic with level by level traversal with printing only the first node of every level */
/* NON-RECURSIVE */
private static List<Integer> getLeftViewNonRecursive(TreeNode root) {
List<Integer> values = new LinkedList<>();
if (root == null) return values;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (true) {
boolean isFirstNodeFromLeftSide = true;
int nodeCount = queue.size();
if (nodeCount == 0) break;
while (nodeCount > 0) {
TreeNode currNode = queue.poll();
if (isFirstNodeFromLeftSide) {
values.add(currNode.data);
isFirstNodeFromLeftSide = false;
}
if (currNode != null && currNode.left != null) queue.add(currNode.left);
if (currNode != null && currNode.right != null) queue.add(currNode.right);
nodeCount--;
}
}
return values;
}
/* RECURSIVE */
private static int maxLevelSeenSoFar = 0;
private static List<Integer> getLeftViewRecursive(TreeNode root) {
List<Integer> values = new LinkedList<>();
getLeftViewUtil(root, 1, values);
maxLevelSeenSoFar = 0;
return values;
}
private static void getLeftViewUtil(TreeNode root, int currLevel, List<Integer> values) {
if(root == null) return;
if(maxLevelSeenSoFar < currLevel) {
values.add(root.data);
maxLevelSeenSoFar = currLevel;
}
getLeftViewUtil(root.left, currLevel + 1, values);
getLeftViewUtil(root.right, currLevel + 1, values);
/* if we traverse the right side child first then we will get right view of the tree. */
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-- > 0) {
String input = br.readLine().trim();
TreeNode root = buildTree(input);
System.out.println("Non recursive ");
List<Integer> output1 = getLeftViewNonRecursive(root); /* non recursive call*/
print(output1);
System.out.println("Recursive ");
List<Integer> output2 = getLeftViewRecursive(root); /* recursive call */
print(output2);
}
}
private static void print(List<Integer> output) {
StringBuilder sb = new StringBuilder();
for (int value : output) {
sb.append(value).append(" ");
}
System.out.println(sb);
sb.setLength(0);
}
private static TreeNode buildTree(String str) {
if(str.length()==0 || str.charAt(0)=='N') return null;
String[] ip = str.split("\\s+");
TreeNode root = new TreeNode(Integer.parseInt(ip[0]));
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int i = 1;
while(!queue.isEmpty() && i < ip.length) {
TreeNode currNode = queue.poll();
String currData = ip[i];
if(!currData.equals("N")) {
currNode.left = new TreeNode(Integer.parseInt(currData));
queue.add(currNode.left);
}
i++;
if(i >= ip.length) break;
currData = ip[i];
if(!currData.equals("N")) {
currNode.right = new TreeNode(Integer.parseInt(currData));
queue.add(currNode.right);
}
i++;
}
return root;
}
private static class TreeNode {
private int data;
private TreeNode left, right;
TreeNode(int data) {
this.data = data;
left = right = null;
}
}
}