class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
lOBhelp(res,root,0);
reverse(res.begin(),res.end());
return res;
}
void lOBhelp(vector<vector<int>> &res,TreeNode *r,int depth){
if(r==NULL) return;
if(depth+1>res.size()) res.resize(depth+1);
res[depth].push_back(r->val);
lOBhelp(res,r->left,depth+1);
lOBhelp(res,r->right,depth+1);
}
};
107. 二叉树的层次遍历 II
最新推荐文章于 2024-12-03 10:35:57 发布
本文介绍了一种实现二叉树层次遍历的方法,并通过递归的方式将每一层节点的值存储到列表中,最后反转列表以实现从底层到顶层的遍历顺序。

5340

被折叠的 条评论
为什么被折叠?



