Skip to content

Commit 26acd40

Browse files
Add files via upload
1 parent bfd183a commit 26acd40

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.company;
2+
3+
import java.util.LinkedList;
4+
import java.util.Map;
5+
import java.util.Queue;
6+
import java.util.TreeMap;
7+
8+
public class BottomViewOfBinaryTree {
9+
Node root=null;
10+
public void topView(Node root) {
11+
class QueueObj {
12+
Node node;
13+
int hd;
14+
QueueObj (Node root, int hd) {
15+
this.node=root;
16+
this.hd=hd;
17+
}
18+
}
19+
Queue<QueueObj> queue=new LinkedList<>();
20+
Map<Integer,Node> topViewMap = new TreeMap<Integer,Node>();
21+
if(root==null){
22+
return;
23+
}else{
24+
queue.add(new QueueObj(root,0));
25+
}
26+
27+
while(!queue.isEmpty()){
28+
QueueObj tempnode=queue.poll();
29+
topViewMap.put(tempnode.hd,tempnode.node);
30+
if(tempnode.node.left!=null)
31+
queue.add(new QueueObj(tempnode.node.left,tempnode.hd-1));
32+
if(tempnode.node.right!=null)
33+
queue.add(new QueueObj(tempnode.node.right,tempnode.hd+1));
34+
}
35+
for(Map.Entry<Integer,Node> entry: topViewMap.entrySet()){
36+
System.out.println(entry.getValue().data);
37+
}
38+
}
39+
40+
41+
public static void main (String[] args) {
42+
BottomViewOfBinaryTree bt = new BottomViewOfBinaryTree();
43+
bt.root=new Node(5);
44+
bt.root.left=new Node(3);
45+
bt.root.right=new Node(7);
46+
bt.root.left.left=new Node(1);
47+
bt.root.left.right=new Node(4);
48+
bt.root.right.left=new Node(6);
49+
bt.root.right.right=new Node(10);
50+
bt.topView(bt.root);
51+
}
52+
}
53+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.company;
2+
3+
import com.sun.source.tree.Tree;
4+
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
import java.util.Queue;
8+
import java.util.TreeMap;
9+
class Node{
10+
int data;
11+
Node left,right;
12+
Node(){
13+
}
14+
Node(int data){
15+
this.data=data;
16+
this.left=null;
17+
this.right=null;
18+
}
19+
}
20+
21+
public class TopViewOfBinaryTree {
22+
Node root=null;
23+
public void topView(Node root) {
24+
class QueueObj {
25+
Node node;
26+
int hd;
27+
QueueObj (Node root, int hd) {
28+
this.node=root;
29+
this.hd=hd;
30+
}
31+
}
32+
Queue<QueueObj> queue=new LinkedList<>();
33+
Map<Integer,Node> topViewMap = new TreeMap<Integer,Node>();
34+
if(root==null){
35+
return;
36+
}else{
37+
queue.add(new QueueObj(root,0));
38+
}
39+
40+
while(!queue.isEmpty()){
41+
QueueObj tempnode=queue.poll();
42+
if(!topViewMap.containsKey(tempnode.hd))
43+
topViewMap.put(tempnode.hd,tempnode.node);
44+
if(tempnode.node.left!=null)
45+
queue.add(new QueueObj(tempnode.node.left,tempnode.hd-1));
46+
if(tempnode.node.right!=null)
47+
queue.add(new QueueObj(tempnode.node.right,tempnode.hd+1));
48+
}
49+
for(Map.Entry<Integer,Node> entry: topViewMap.entrySet()){
50+
System.out.println(entry.getValue().data);
51+
}
52+
}
53+
54+
55+
public static void main (String[] args) {
56+
TopViewOfBinaryTree bt = new TopViewOfBinaryTree();
57+
bt.root=new Node(5);
58+
bt.root.left=new Node(3);
59+
bt.root.right=new Node(7);
60+
bt.root.left.left=new Node(1);
61+
bt.root.left.right=new Node(4);
62+
bt.root.right.left=new Node(6);
63+
bt.root.right.right=new Node(10);
64+
bt.topView(bt.root);
65+
}
66+
}
67+

0 commit comments

Comments
 (0)