/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型vector<vector<>> */ vector<vector<int> > levelOrder(TreeNode* root) { vector<vector<int>> res; queue<TreeNode*> q; if(!root) return {}; q.push(root); while(!q.empty()){ vector<int> out; for(int i=q.size(); i>0; i--){ TreeNode* t = q.front(); out.push_back(t->val); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } res.push_back(out); } return res; // write code here } };
https://www.cnblogs.com/grandyang/p/4051321.html