My simple c++ solution


  • 0
    K
    class Solution {
    public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        queue<TreeNode * >queue;
       
        vector<vector<int>>res;
        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();
                 level.push_back(temp->val);
                 queue.pop();
                 size--;
                 if(temp->left)queue.push(temp->left);
                  if(temp->right)queue.push(temp->right);
             }
             res.insert(res.begin(), level);
            
        }
        
        return res;
    }
    

    };


Log in to reply
 

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