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

把二叉树打印成多行

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

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
#include <vector>
class Solution {
public:
        vector<vector<int>> ans;
        vector<vector<int>> Print(TreeNode* pRoot,int x=0) {
            if(pRoot==nullptr){
                return ans;
            }
            if(ans.size()<x+1){
                vector<int> t;
                ans.push_back(t);
            }
            ans[x].push_back(pRoot->val);
            Print(pRoot->left,x+1);
            Print(pRoot->right,x+1);
            return ans;
        }
    
};

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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