-
Notifications
You must be signed in to change notification settings - Fork 13
/
Tree_TopViewOfBinaryTree.java
122 lines (96 loc) · 3.86 KB
/
Tree_TopViewOfBinaryTree.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// https://practice.geeksforgeeks.org/problems/top-view-of-binary-tree/1/
// https://www.youtube.com/watch?v=c3SAvcjWb1E (BEST Video tutorial)
// https://www.geeksforgeeks.org/print-nodes-top-view-binary-tree/
// https://practice.geeksforgeeks.org/problems/print-a-binary-tree-in-vertical-order/1 (Similar question)
// https://practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1/ (Similar question)
public class Tree_TopViewOfBinaryTree {
static class Pair {
private TreeNode node;
private int horizontalDistance;
Pair(TreeNode node, int horizontalDistance) {
this.node = node;
this.horizontalDistance = horizontalDistance;
}
}
/* level order traversal is applied here
* This below code is very similar to vertical order traversal
* In top view, we only take the first node of each list of nodes */
private static List<Integer> getTopView(TreeNode root) {
List<Integer> output = new ArrayList<>();
if (root == null) return output;
Map<Integer, Integer> treeMap = new TreeMap<>(); /* TreeMap is sorted according to the natural ordering of its keys */
Queue<Pair> queue = new LinkedList<>();
queue.add(new Pair(root, 0));
while (!queue.isEmpty()) {
int queueSize = queue.size();
while (queueSize-- > 0) {
Pair pair = queue.poll();
TreeNode currNode = pair.node;
int horizontalDistance = pair.horizontalDistance;
if (!treeMap.containsKey(horizontalDistance)) {
treeMap.put(horizontalDistance, currNode.data);
}
if (currNode.left != null) queue.add(new Pair(currNode.left, horizontalDistance - 1));
if (currNode.right != null) queue.add(new Pair(currNode.right, horizontalDistance + 1));
}
}
output.addAll(treeMap.values());
return output;
}
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 s = br.readLine().trim();
TreeNode root = buildTree(s);
List<Integer> output = getTopView(root);
printTree(output);
}
}
private static void printTree(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 currVal = ip[i];
if (!currVal.equals("N")) {
currNode.left = new TreeNode(Integer.parseInt(currVal));
queue.add(currNode.left);
}
i++;
if (i >= ip.length) break;
currVal = ip[i];
if (!currVal.equals("N")) {
currNode.right = new TreeNode(Integer.parseInt(currVal));
queue.add(currNode.right);
}
i++;
}
return root;
}
private static class TreeNode {
private int data;
private TreeNode left;
private TreeNode right;
TreeNode(int data) {
this.data = data;
left = right = null;
}
}
}