Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> levelOrder(TreeNode* root) {
- if(root == NULL)return {};
- vector<vector<int>> ans;
- vector<int> v;
- int n;
- queue<TreeNode*> q;
- TreeNode* p;
- q.push(root);
- while(!q.empty()){
- n = q.size();
- v.clear();
- while(n--){
- p = q.front();
- q.pop();
- v.push_back(p->val);
- if(p->left){
- q.push(p->left);
- }if(p->right){
- q.push(p->right);
- }
- }
- ans.push_back(v);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment