class Solution { public: vector<vector<int> > levelOrder1(TreeNode* root) { vector<vector<int>> res; if (root == nullptr) return res; queue<TreeNode*> q; q.push(root); TreeNode* cur; while (!q.empty()) { vector<int> ret; int n = q.size(); for (int i = 0; i < n; ++i) { cur = q.front(); q.pop(); ret.push_back(cur->val); if (cur->left) q.push(cur->left); if (cur->right) q.push(cur->right); } res.push_back(ret); } return res; } void levelOrder_(TreeNode* root,int n,vector<vector<int>>& res) { if(root==nullptr) return; else { if(res.size()<n) res.push_back(vector<int>{}); res[n-1].push_back(root->val); } levelOrder_(root->left, n+1, res); levelOrder_(root->right, n+1, res); } vector<vector<int> > levelOrder(TreeNode* root) { vector<vector<int>> res; levelOrder_(root,1,res); return res; } };