题目
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其自底向上的层次遍历为:
[
[15,7],
[9,20],
[3]
]
相比于LeetCode 102 二叉树的层次遍历只是多了一个翻转。
/**
* 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>> levelOrderBottom(TreeNode* root) {
vector< vector<int> > res;
if(root == NULL)
return res;
queue < pair<TreeNode*, int> > q; //保存节点和节点所在的层
q.push(make_pair(root, 0));
while(!q.empty()){
TreeNode* node = q.front().first;
int level = q.front().second;
q.pop();
if(level == res.size())
res.push_back(vector<int>() );
res[level].push_back(node->val);
if(node->left != NULL)
q.push(make_pair(node->left, level + 1));
if(node->right != NULL)
q.push(make_pair(node->right, level + 1));
}
vector<vector<int>> resBottom;
for(int i = res.size() -1;i >= 0; i--){
vector<int> tmp = res[i];
resBottom.push_back(tmp);
}
return resBottom;
}
};