You are on page 1of 13

Views of Tree

View of Tree
Given the root of a binary tree, imagine yourself standing on the right/left side of it, return the values of the nodes
you can see ordered from top to bottom.
View of Tree
Given the root of a binary tree, imagine yourself standing on the top/bottom side of it, return the values of the
nodes you can see ordered from top to bottom.
1 import java.util.*;
2 import java.util.Map.Entry;
3 class Node {
4 int data,hd;
5 Node left, right;
6 public Node(int data){
7 this.data = data;
8 left = right = null;
9 this.hd = INT_MAX;
10 }
11 }
12
13 class Main {
14 static Node root;
15 private List<Integer> path1 = new ArrayList<>();
16 private List<Integer> path2 = new ArrayList<>();
17 static Node build(String s[]){
18 if(s[0]=="N"||s.length==0)
19 return null;
20 Node root=new Node(Integer.parseInt(s[0]));
21 Queue<Node> q=new LinkedList<Node>();
22 q.add(root);
23 int i=1;
24 while(!q.isEmpty() && i<s.length){
25 Node curr=q.poll();
26 String cval=s[i];
27 if(!cval.equals("N")){
28 int h=Integer.parseInt(cval);
29 curr.left=new Node(h);
30 q.add(curr.left);
31 }
32 i++;
33 if(i >= s.length)
34 break;
35 cval = s[i];
36 if(!cval.equals("N")){
37 int h=Integer.parseInt(cval);
38 curr.right=new Node(h);
39 q.add(curr.right);
40 }
41 i++;
42 }
43 return root;
44 }
45 //Right View
46 void rightview(Node root){
47 if (root == null)
48 return;
49 Queue<Node> q = new LinkedList<>();
50 q.add(root);
51 while (!q.isEmpty()) {
52 int n = q.size();
53 for (int i = 0; i < n; i++) {
54 Node curr = q.peek();
55 q.remove();
56 if (i == n - 1) {
57 System.out.print(curr.data + " ");
58 if (curr.left != null)
59 q.add(curr.left);
60 if (curr.right != null)
61 q.add(curr.right);
62 }
63 }
64 }
65
66
67 //Left View
68 void leftview(Node root){
69 if (root == null)
70 return;
71 Queue<Node> queue = new LinkedList<>();
72 queue.add(root);
73 while (!queue.isEmpty()) {
74 int n = queue.size();
75 for (int i = 1; i <= n; i++) {
76 Node temp = queue.poll();
77 if (i == 1)
78 System.out.print(temp.data + " ");
79 if (temp.left != null)
80 queue.add(temp.left);
81 if (temp.right != null)
82 queue.add(temp.right);
83 }
84 }
85 }
86
87
88
89 //Top View
90 static class QueueObj {
91 Node node;
92 int hd;
93 QueueObj(Node node, int hd){
94 this.node = node;
95 this.hd = hd;
96 }
97 }
98 static void topview(Node root){
99 if (root == null)
100 return;
101 Queue<QueueObj> q = new LinkedList<>();
102 Map<Integer, Integer> map = new HashMap<>();
103 int min = 0;
104 int max = 0;
105 q.add(new QueueObj(root, 0));
106 while (!q.isEmpty()) {
107 QueueObj curr = q.poll();
108 if (!map.containsKey(curr.hd))
109 map.put(curr.hd, curr.node.data);
110
111 if (curr.node.left != null) {
112 min = Math.min(min, curr.hd - 1);
113 q.add(new QueueObj(curr.node.left,curr.hd - 1));
114 }
115 if (curr.node.right != null) {
116 max = Math.max(max, curr.hd + 1);
117 q.add(new QueueObj(curr.node.right,curr.hd + 1));
118 }
119 }
120 for (; min <= max; min++)
121 System.out.print(map.get(min) + " ");
122 }
123
124 //Bottom View
125 static void bottomview(Node root){
126 if (root == null)
127 return;
128 int hd = 0;
129 Map<Integer, Integer> map = new TreeMap<>();
130 Queue<Node> queue = new LinkedList<Node>();
131 root.hd = hd;
132 queue.add(root);
133 while (!queue.isEmpty()){
134 Node temp = queue.remove();
135 hd = temp.hd;
136 map.put(hd, temp.data);
137 if (temp.left != null){
138 temp.left.hd = hd-1;
139 queue.add(temp.left);
140 }
141 if (temp.right != null)
142 {
143 temp.right.hd = hd+1;
144 queue.add(temp.right);
145 }
146 }
147 Set<Entry<Integer, Integer>> set = map.entrySet();
148 Iterator<Entry<Integer, Integer>> iterator = set.iterator();
149 while (iterator.hasNext()){
150 Map.Entry<Integer, Integer> me = iterator.next();
151 System.out.print(me.getValue()+" ");
152 }
153 }
154
155 //main method
156 public static void main(String[] args){
157 Scanner sc=new Scanner(System.in);
158 int i;
159 Main ob = new Main();
160 String s[]=sc.nextLine().split(" ");
161 root = build(s);
162 ob.rightview(root);
163 System.out.println();
164 ob.leftview(root);
165 System.out.println();
166 ob.topview(root);
167 System.out.println();
168 ob.bottomview(root);
169 }
170 }
171
172
173
174
175
176
THANK YOU

You might also like