Why this code is giving runtime error? Loops are working well.


  • 0
    B
    class Solution {
          
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            TreeNode *current,*p;
            vector<int> k1;
            if(root==NULL)
            return k1;;
          
            current=root;
            while(current!=NULL){
               
                if(current->left==NULL){
                    k1.push_back(current->val);
                 //  cout<<current->val<<" ";
                    current=current->right;
                }
                else{
                  p=current->left;  
                  while(p->right!=NULL && p->right!=current){
                      p=p->right;
                      
                  }
                  if(p->right==current){
                      k1.push_back(current->val);
                   //  cout<<current->val<<" ";
                      p->right==NULL;
                      current=current->right;
                  }
                  else{
                      p->right=current;
                      current=current->left;
                  }
                }
               
            }
          // cout<<"returned";
            return k1;
            
        }
    };

Log in to reply
 

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