Easy Backtracking Trees Solution


#1

void f(vector<vector >&ans,int level,TreeNode A)
{
if(A==NULL)return;
if(ans.size()<=level)
{
ans.push_back(vector(1,A->val));
}
else ans[level].push_back(A->val);
f(ans,level+1,A->left);
f(ans,level+1,A->right);
}
vector<vector > Solution::levelOrder(TreeNode
A)
{
vector<vector >ans;
f(ans,0,A);
return ans;
}