My C++ Solution use childSize and parentSize


  • 1
    T
    class Solution {
    public:
        vector<vector<int>> levelOrderBottom(TreeNode* root)
        {
            vector<vector<int>>v;
            if(!root)return v;
            queue<TreeNode*>q;
            q.push(root);
            int child=0,parent=1;
            vector<int>vLevel;
            while(!q.empty())
            {
                TreeNode* tmp = q.front();
                vLevel.push_back(tmp->val);
                q.pop();
                if (tmp->left){q.push(tmp->left);child++;}
                if (tmp->right){q.push(tmp->right);child++;}
                parent--;
                // level end
                if (parent==0)
                {
                    parent=child;
                    child=0;
                    v.push_back(vLevel);
                    vLevel.clear();   
                }
            }
            reverse(v.begin(),v.end());
            return v;
        }
    };

Log in to reply
 

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