class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型vector<vector<>>
*/
vector<vector<int> > levelOrder(TreeNode* root) {
// write code here
vector<vector<int>> a;
cengorder(root, 0, a);
return res;
}
void cengorder(TreeNode* root, int depth, vector<vector<int>> &a)
{
if (!root)
return;
if (a.size() == depth)
a.push_back(vector<int>());
a[depth].push_back(root->val);
for (TreeNode* c : root->children) //就这儿不一样,因为不知道有几个孩子,所以需要 for循环
{
cengorder(c, depth + 1, a);
}
}
};
N叉树遍历(层次)递归法
最新推荐文章于 2024-02-19 16:18:50 发布