/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ #include <iterator> #include <vector> class Solution { public: /** * * @param root TreeNode类 * @return int整型vector<vector<>> */ vector<vector<int> > levelOrder(TreeNode* root) { vector<vector<int>> ans; if(root==nullptr) return ans ; queue<TreeNode*> Q; Q.push(root); vector<int>t; while(!empty(Q)){ int size=Q.size(); while(size--){ root=Q.front(); Q.pop(); t.push_back(root->val); if(root->left) Q.push(root->left); if(root->right) Q.push(root->right); } ans.push_back(t); t.clear(); } return ans; } };