看着像BFS,其实是DFS。
学到了一句,二维数组插入空一维数组:
v.push_back(vector<int>());
class Solution {
public:
vector<vector<int> > v;
void dfs(TreeNode* p,int deep){
if(p==NULL) return ;
if(deep>=v.size()){
v.push_back(vector<int>());
}
v[deep].push_back(p->val);
dfs(p->left,deep+1);
dfs(p->right,deep+1);
}
vector<vector<int> > Print(TreeNode* pRoot) {
dfs(pRoot,0);
return v;
}
};