标签:
]
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
vector<int> temp;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
TreeNode *node;
while(!q.empty())
{
queue<TreeNode*> q1;
while(!q.empty())
{
node=q.front();
temp.push_back(node->val);
q.pop();
if(node->left!=NULL)
{
q1.push(node->left);
}
if(node->right!=NULL)
{
q1.push(node->right);
}
}
res.push_back(temp);
q=q1;
temp.clear();
}
return res;
}
};
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).
For 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] ]
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
vector<int> temp;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
TreeNode* node;
while(!q.empty())
{
queue<TreeNode*> q1;
while(!q.empty())
{
node=q.front();
q.pop();
temp.push_back(node->val);
if(node->left!=NULL)
q1.push(node->left);
if(node->right!=NULL)
q1.push(node->right);
}
res.push_back(temp);
temp.clear();
q=q1;
}
reverse(res.begin(),res.end());
return res;
}
};Binary Tree Level Order Traversal 、Binary Tree Level Order Traversal II
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/45533443