Binary Tree Vertical Order Traversal -- LeetCode
2016-08-24 09:41
246 查看
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).
If two nodes are in the same row and column, the order should be from left to right.
Examples:
Given binary tree
return its vertical order traversal as:
Given binary tree
return its vertical order traversal as:
Given binary tree
return its vertical order traversal as:
思路:bsf。然后用链表暂时存储结果。假设当前链表节点的iterator为it,则它的前一个节点的iterator可以用std::prev(it, 1)获得,它的下一个节点的iterator可以用std::next(it, 1)获得。
If two nodes are in the same row and column, the order should be from left to right.
Examples:
Given binary tree
[3,9,20,null,null,15,7],
3 /\ / \ 9 20 /\ / \ 15 7
return its vertical order traversal as:
[ [9], [3,15], [20], [7] ]
Given binary tree
[3,9,8,4,0,1,7],
3 /\ / \ 9 8 /\ /\ / \/ \ 4 01 7
return its vertical order traversal as:
[ [4], [9], [3,0,1], [8], [7] ]
Given binary tree
[3,9,8,4,0,1,7,null,null,null,2,5](0's right child is 2 and 1's left child is 5),
3 /\ / \ 9 8 /\ /\ / \/ \ 4 01 7
/\
/ \
5 2
return its vertical order traversal as:
[ [4], [9,5], [3,0,1], [8,2], [7] ]
思路:bsf。然后用链表暂时存储结果。假设当前链表节点的iterator为it,则它的前一个节点的iterator可以用std::prev(it, 1)获得,它的下一个节点的iterator可以用std::next(it, 1)获得。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int>> verticalOrder(TreeNode* root) { vector<vector<int> > res; if (root == NULL) return res; list<vector<int> > columns; queue<TreeNode*> nodes; queue<list<vector<int> >::iterator> iterators; columns.push_back(vector<int>(1, root->val)); nodes.push(root); iterators.push(columns.begin()); while (!nodes.empty()) { TreeNode* cur = nodes.front(); nodes.pop(); auto it = iterators.front(); iterators.pop(); if (cur->left) { if (it == columns.begin()) columns.push_front(vector<int>(1, cur->left->val)); else std::prev(it, 1)->push_back(cur->left->val); nodes.push(cur->left); iterators.push(std::prev(it, 1)); } if (cur->right) { if (std::next(it, 1) == columns.end()) columns.push_back(vector<int>(1, cur->right->val)); else std::next(it, 1)->push_back(cur->right->val); nodes.push(cur->right); iterators.push(std::next(it, 1)); } } for (auto it = columns.begin(); it != columns.end(); it++) res.push_back(*it); return res; } };
相关文章推荐
- Leetcode: Binary Tree Vertical Order Traversal
- [Leetcode]Binary Tree Vertical Order Traversal
- LeetCode. 314 Binary Tree Vertical Order Traversal
- [LeetCode314]Binary Tree Vertical Order Traversal
- Leetcode Binary Tree Vertical Order Traversal
- LeetCode Binary Tree Vertical Order Traversal
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
- LeetCode-Binary Tree Vertical Order Traversal
- [LeetCode]Binary Tree Vertical Order Traversal
- 刷题leetcode102 Binary Tree Level Order Traversal
- LeetCode Binary Tree Level Order Traversal
- 【Leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- leetcode_102_Binary Tree Level Order Traversal
- leetcode 094 Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- [C++]LeetCode: 32 Binary Tree Level Order Traversal II
- leetcode-java-106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Binary Tree Zigzag Level Order Traversal
- LeetCode:Binary Tree Level Order Traversal