#include #include #include #include #include #include #include using namespace std; class Node { public: int left; int right; int hd; int data; Node(int data, int left, int right): data(data),right(right),left(left){} }; vector v; void topview(Node root) { if(root.data==-1) return; queueq; map m; int hd=0; root.hd=hd; q.push(root); while(q.size()) { hd=root.hd; if(m.count(hd)==0) m[hd]=root.data; if(root.left!=-1) { v[root.left].hd=hd-1; q.push(v[root.left]); } if(root.right!=-1) { v[root.right].hd=hd+1; q.push(v[root.right]); } q.pop(); root=q.front(); } for(auto i=m.begin();i!=m.end();i++) { cout<second<<" "; } } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n,a,b; cin>>n; for(int i=0;i>a>>b; Node curr(i,a,b); v.push_back(curr); } topview(v[0]); return 0; }