My simple c++ solution


  • 5
    K
    class Solution {
    public:
    vector<vector<int>> levelOrder(TreeNode* root) {
       
        vector<vector<int>>res;
        queue<TreeNode*>queue;
        int size;
        if(!root)return res;
        queue.push(root);
        while(!queue.empty())
        {
             vector<int>level;
            size=queue.size();
            while(size>0)
            {
                TreeNode * temp=queue.front();
                queue.pop();
                size--;
                level.push_back(temp->val);
                if(temp->left)queue.push(temp->left);
                if(temp->right)queue.push(temp->right);
            }
            res.push_back(level);
        }
          return res;
        
    }
    

    };


  • 0
    Z

    I just want to say : GOOD IDEA!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.