Getting runtime error for one testcase


  • 0
    S

    can someone please tell what is the mistake i am doing.Thanks for help

     public TreeNode DeleteNode(TreeNode root, int key) {
            
            if(root==null) return root;
            
            
            if(root.val<key){
                root.right= DeleteNode(root.right,key);
                return root;
            }
            else if(root.val>key){
                root.left= DeleteNode(root.left,key);
                return root;
            }
            else{
                
                if(root.left==null && root.right==null)
                  return null;
                 else if(root.left==null || root.right==null){
                     return root.left!=null?root.left:root.right;
                 }
                 else{
                     TreeNode temp=root.right;
                     while(temp.left!=null){
                         temp=temp.left;
                     }
                     root.val=temp.val;
                     
                     root.right=DeleteNode(root.right,temp.val);
                     return root;
                 }
                 
                
            }
            
            
            
            
            
        }
    

Log in to reply
 

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