class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
if(!root) return {};
queue<TreeNode*> q;
q.push(root);
vector<vector<int>> ret;
while(!q.empty())
{
vector<int> path;
int n = q.size();
for(int i=0;i<n;i++)
{
auto tmp = q.front();
q.pop();
path.push_back(tmp->val);
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
ret.push_back(path);
}
reverse(ret.begin(),ret.end());
return ret;
}
};
二叉树层序遍历
最新推荐文章于 2024-07-24 22:13:06 发布