#70 Binary Tree Level Order Traversal II
2016-08-28 06:04
316 查看
题目描述:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
Have you met this question in a real interview?
Yes
Example
Given binary tree
return its bottom-up level order traversal as:
题目思路:
这题和#71基本一样,只是在BFS之后,把得到的ans reverse一下即可(因为要求bottom to top).
Mycode(AC = 21ms):
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root : The root of binary tree.
* @return : buttom-up level order a list of lists of integer
*/
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
// write your code here
vector<vector<int>> ans;
if (root == NULL) return ans;
// initialize 2 queues, one to
// store tree nodes, one to store
// level/height of corresponding node
queue<TreeNode *> helper;
helper.push(root);
queue<int> levels;
levels.push(0);
while (!helper.empty()) {
TreeNode *node = helper.front();
helper.pop();
int level = levels.front();
levels.pop();
// push the node into ans, according
// to its level
if (ans.size() <= level) {
ans.push_back({node->val});
}
else {
ans[level].push_back(node->val);
}
// push the left/right tree and their
// corresponding level into queue
if (node->left) {
helper.push(node->left);
levels.push(level + 1);
}
if (node->right) {
helper.push(node->right);
levels.push(level + 1);
}
}
// reverse the final ans, because it requires bottom to top
reverse(ans.begin(), ans.end());
return ans;
}
};
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
Have you met this question in a real interview?
Yes
Example
Given binary tree
{3,9,20,#,#,15,7},
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
题目思路:
这题和#71基本一样,只是在BFS之后,把得到的ans reverse一下即可(因为要求bottom to top).
Mycode(AC = 21ms):
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root : The root of binary tree.
* @return : buttom-up level order a list of lists of integer
*/
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
// write your code here
vector<vector<int>> ans;
if (root == NULL) return ans;
// initialize 2 queues, one to
// store tree nodes, one to store
// level/height of corresponding node
queue<TreeNode *> helper;
helper.push(root);
queue<int> levels;
levels.push(0);
while (!helper.empty()) {
TreeNode *node = helper.front();
helper.pop();
int level = levels.front();
levels.pop();
// push the node into ans, according
// to its level
if (ans.size() <= level) {
ans.push_back({node->val});
}
else {
ans[level].push_back(node->val);
}
// push the left/right tree and their
// corresponding level into queue
if (node->left) {
helper.push(node->left);
levels.push(level + 1);
}
if (node->right) {
helper.push(node->right);
levels.push(level + 1);
}
}
// reverse the final ans, because it requires bottom to top
reverse(ans.begin(), ans.end());
return ans;
}
};
相关文章推荐
- [LeetCode] Unique Binary Search Trees
- [LeetCode] Unique Binary Search Trees II
- [LeetCode] Sum Root to Leaf Numbers
- [LeetCode] Binary Tree Preorder Traversal
- [LeetCode] Binary Tree Postorder Traversal
- [LeetCode] Binary Tree Inorder Traversa
- [LeetCode] Convert Sorted Array to Binary Search Tree
- [LeetCode] Convert Sorted List to Binary Search Tree
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- [LeetCode] Flatten Binary Tree to Linked List
- [LeetCode] Balanced Binary Tree
- [LeetCode] Binary Tree Level Order Traversal
- [LeetCode] Binary Tree Level Order Traversal II
- LintCode算法题解
- LintCode Majority Number iii
- LintCode-最大数
- lintCode Intersection of Two Arrays II