比之字形打印二叉树还要少一个动作,那个题是简单,这个是中等???
vector<vector<int> > res; vector<vector<int> > Print(TreeNode* pRoot) { PrintLevel(pRoot, 0); return res; } void PrintLevel(TreeNode* pRoot,int level){ if(pRoot == NULL){ return; } if(res.size() < level + 1){ res.push_back({}); } res[level].push_back(pRoot->val); PrintLevel(pRoot->left,level+1); PrintLevel(pRoot->right,level+1); }