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

把二叉树打印成多行

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;
        }
    
};

全部评论

相关推荐

ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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