题解 | #把二叉树打印成多行#

把二叉树打印成多行

http://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            if (!pRoot) return {};
            vector<vector<int>> ans;
            queue<TreeNode*> que;
            que.push(pRoot);
            while (!que.empty()) {
                vector<int> t;
                for (int i = que.size() - 1; i >= 0; i--) {
                    auto f = que.front();
                    que.pop();
                    t.push_back(f->val);
                    if (f->left) que.push(f->left);
                    if (f->right) que.push(f->right);
                }   
                ans.push_back(t);
            }
            return ans;
        }
    
};
全部评论

相关推荐

Cherrycola01:0实习 0项目 约等于啥也没有啊 哥们儿这简历认真的吗
点赞 评论 收藏
分享
牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务