题解 | #把二叉树打印成多行#
把二叉树打印成多行
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; } };