What should we return if the root is NULL?


  • 0
    K

    What should we return if the root is NULL?. (Im coding in cpp) . My code handles everything except the null case.

    class BSTIterator {
    public:
    vector<int> result;
    int current=0;
    TreeNode* node;
    void inorder(TreeNode *node)
    {

        if(node!=NULL)
        {
            inorder(node->left);
            result.push_back(node->val);
            inorder(node->right);
        }else{
            return;
        }
    }
    
    
    BSTIterator(TreeNode *root) {
        node=root;
        inorder(root);
        
        for(int i=0;i<result.size();i++)
        {
            cout<<result[i];
        }
    }
    
    /** @return whether we have a next smallest number */
    bool hasNext() {
         if(current>result.size()-1)
         {
             return false;
         }else{
             return true;
         }
    }
    
    /** @return the next smallest number */
    int next() {
        current++;
        
        if(result.empty()) 
        {
            //return what?
        }
        
        return result[current-1];
    }
    

    };


Log in to reply
 

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