/**
* 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>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
int toprint=1;
int nextlevel=0;
vector<int> one;
while(!q.empty())
{
TreeNode * cur = q.front();
one.push_back(cur->val);
if(cur->left)
{
q.push(cur->left);
nextlevel++;
}
if(cur->right)
{
q.push(cur->right);
nextlevel++;
}
toprint--;
q.pop();
if(toprin
leetcode#分层打印二叉树
最新推荐文章于 2022-10-13 18:28:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)