Got runtime error, but why?


  • 0
    E

    class Solution {
    public:
    vector<vector<int> > levelOrder(TreeNode root) {
    queue<TreeNode
    > nodes;
    vector<vector<int>> levelTree;

        nodes.push(root);
        int i = 0, level = 0;
        
        TreeNode* curr;
        while(!nodes.empty())
        {
            i++;
            curr = nodes.front();
            if(curr)
            {
                //if(curr->left)
                    nodes.push(curr->left);
                //if(curr->right)
                    nodes.push(curr->right);
    
                if(i > 2^level)
                    level++;
                levelTree[level].push_back(curr->val);
            }
            nodes.pop();
        }
        return levelTree;
        
    }
    

    };


Log in to reply
 

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