/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if(root == NULL) return res; queue<TreeNode*> que; que.push(root); int gcnt = 1; while(!empty(que)){ int cnt = 0; vector<int> tres; for(int i=0;i < gcnt;i++){ TreeNode* temp = que.front(); if(temp->left != NULL){ que.push(temp->left); cnt++; } if(temp->right != NULL){ que.push(temp->right); cnt++; } tres.push_back(temp->val); que.pop(); } gcnt = cnt; res.push_back(tres); } return res; } };
_