Vertical Order Traversal of a Binary Tree
https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/
https://gist.github.com/SuryaPratapK/d73b9a97c2c8a2af762af106b794ad2d //soln
| class Solution { |
| map<int,map<int,multiset<int>>> mymap; |
| void solve(TreeNode *curr,int col,int row) |
| { |
| if(!curr) |
| return; |
| |
| mymap[col][row].insert(curr->val); |
| solve(curr->left,col-1,row+1); |
| solve(curr->right,col+1,row+1); |
| } |
| public: |
| vector<vector<int>> verticalTraversal(TreeNode* root) { |
| solve(root,0,0); |
| |
| vector<vector<int>> ans; |
| for(auto m1: mymap) |
| { |
| ans.push_back(vector<int>()); |
| for(auto m2: m1.second) |
| { |
| for(auto m3: m2.second) |
| ans.back().push_back(m3); |
| } |
| } |
| return ans; |
| } |
| //DFS Method |


Comments
Post a Comment