Java Solution Not Change Value,easy to understand


  • 1
    public class Solution {
         public TreeNode deleteNode(TreeNode root, int key) {
    
            if (root == null)
                return null;
            if (key > root.val)
                root.right = deleteNode(root.right, key);
            else if (key < root.val)
                root.left = deleteNode(root.left, key);
            else {
    
                if (root.left == null)
                    return root.right;
                if (root.right == null)
                    return root.left;
                TreeNode cur = root;
                root = min(root.right);
                root.right = deleteMin(cur.right);
                root.left = cur.left;
    
            }
            return root;
    
        }
    
        public TreeNode min(TreeNode root) {
    
            if (root.left == null)
                return root;
            return min(root.left);
    
        }
    
        public TreeNode deleteMin(TreeNode root) {
    
            if (root.left == null)
                return root.right;
    
            root.left = deleteMin(root.left);
            return root;
        }
    }
    

Log in to reply
 

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