Untitled
plain_text
a month ago
1.0 kB
0
Indexable
Never
#include<map> #include<queue> #include<vector> using namespace std; vector<int> bottomView(TreeNode<int> * root){ vector<int> ans; if(root==NULL) return ans; map<int,vector<int>> topnode; //(distance,topnode) //vector wont work for topnode queue<pair<TreeNode<int>*,int>> q; // (node,horizontal dsit) q.push(make_pair(root,0)); while(!q.empty()){ pair<TreeNode<int>*,int> front=q.front(); //getting the node q.pop(); //pop korte keno BHULE jasss??? TreeNode<int>* tmp=front.first; int dist=front.second; topnode[dist].push_back(tmp->data); if(tmp->left) q.push(make_pair(tmp->left,dist-1)); if(tmp->right) q.push(make_pair(tmp->right,dist+1)); } //now topnode has all topnodes in format (distance,node) for(auto i:topnode){ ans.push_back(i.second.back()); } return ans; } /* we make a DS of (dist,vector) called topnode though it should be named hord */