题目描述:
https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/
解题思路:
使用结构嵌套:map<int, map<int,multiset<int>>> myMap;
第一层Map实现按列排序
第二层map实现按行排序
最里面Multiset实现容纳同行同列值,并排序。
class Solution {
public:map<int, map<int,multiset<int>>> myMap;void findAnsByLDR(TreeNode* node, int row, int col){if (nullptr == node)return;myMap[col][row].insert(node->val);findAnsByLDR(node->left, row + 1, col - 1);findAnsByLDR(node->right, row+1, col + 1);return;}vector<vector<int>> verticalTraversal(TreeNode* root) {findAnsByLDR(root, 0, 0);vector<vector<int>> ret;vector<int> tmp;for (auto pirs : myMap){tmp.clear();for (auto pir : pirs.second){for (auto v : pir.second)tmp.push_back(v);}ret.push_back(tmp);}return ret;}
};