LC 987. Vertical Order Traversal of a Binary Tree
Given a binary tree,return the?vertical order?traversal of its nodes?values. For each node at position? Running a vertical line from? If two nodes have the same position,then the value of the node that is reported first is the value that is smaller. Return an list?of non-empty reports in order of? ? ? ? ? ? class Solution { public: vector<vector<int>> verticalTraversal(TreeNode* root) { map<int,map<int,vector<int>>> mp; vector<vector<int>> ret; helper(root,0,0,mp); for(auto it=mp.begin(); it!=mp.end(); it++){ vector<int> a; for(auto iit=it->second.rbegin(); iit!=it->second.rend(); iit++) { vector<int> tmp = iit->second; sort(tmp.begin(),tmp.end()); for(auto x : tmp) a.push_back(x); } ret.push_back(a); } return ret; } void helper(TreeNode* root,int x,int y,vector<int>>>& mp){ if(!root) return; mp[x][y].push_back(root->val); helper(root->left,x-1,y-1,mp); helper(root->right,x+1,mp); } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |