按层输出二叉树,广度优先。
3
/ \
9 20
/ \
15 7
[
[15,7],
[9,20],
[3]
]
/**
* 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>> rres; void levelTree(TreeNode*root, int level){
if (root == NULL)
return;
if (level == rres.size()){
vector<int> res;
rres.push_back(res);
} rres[level].push_back(root->val);
levelTree(root->left,level+);
levelTree(root->right,level+);
} vector<vector<int>> levelOrderBottom(TreeNode* root) {
levelTree(root,);
//return reverse(rres.begin(),rres.end());
return vector<vector<int> >(rres.rbegin(), rres.rend());
}
};