剑指Offer——JZ60.把二叉树打印成多行【BFS】

题目传送门


在这里插入图片描述


题解

在这里插入图片描述

AC-Code

class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int>> ret;
        queue<TreeNode*> q;
        if(pRoot)    q.push(pRoot);
        while (!q.empty()) {
            int sz = q.size();
            vector<int> ans;
            while (sz--) {
                TreeNode *node = q.front();
                q.pop();
                ans.push_back(node->val);

                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
            ret.push_back(ans);
        }
        return ret;
    }
};

猜你喜欢

转载自blog.csdn.net/Q_1849805767/article/details/106740410