-
Notifications
You must be signed in to change notification settings - Fork 0
/
topview.cpp
101 lines (83 loc) · 1.87 KB
/
topview.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int x){
data=x;
left=NULL;
right=NULL;
}
};
void topView(Node* root){
queue <pair <Node*,int> > q;
if(root==NULL){
return;
}
q.push({root,0});
int left_dist=0;
int right_dist=0;
stack <int> st;
vector <int> v;
while(!q.empty()){
Node* par=q.front().first;
int root_dis=q.front().second;
int hl=root_dis-1;
int hr=root_dis+1;
q.pop();
if(par->left!=NULL){
q.push({par->left,hl});
if(hl<left_dist){
left_dist=hl;
st.push(par->left->data);
}
}
if(par->right){
q.push({par->right,hr});
if(hr>right_dist){
right_dist=hr;
v.push_back(par->right->data);
}
}
}
cout<<"PRINTING TOP VIEW OF BINARY TREE: ";
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}
cout<<root->data<<" ";
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
cout<<"\n";
}
Node* createBst(int arr[],int i,int j){
if(i<=j){
int mid=(i+j)/2;
Node* root=new Node(arr[mid]);
root->left=createBst(arr,i,mid-1);
root->right=createBst(arr,mid+1,j);
return root;
}
return NULL;
}
void inorder(Node* root){
if(root==NULL){
return;
}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
int main(){
int arr[]={1,2,5,7,10,13,14,15,22};
int i=0;
int j=8;
Node* root=createBst(arr,i,j);
inorder(root);
cout<<"\n";
topView(root);
return 0;
}