Leetcode 450. Delete Node in a BST

题目链接:https://leetcode.cn/problems/delete-node-in-a-bst/

方法一 迭代

1 方法思想

2 代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
            // 迭代:两个父节点
        if (root == null) return root;
        TreeNode curParent = null;
        TreeNode cur = root;
        while (cur != null && cur.val != key) {
            curParent = cur;
            if (cur.val > key) {
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        if (cur == null) {
            return root;
        } else {
            if (cur.right == null && cur.left == null) {
                cur = null;
                //return root;
            }else if (cur.left == null) {
                cur = cur.right;
                // return root;
            }else if (cur.right == null) {
                cur = cur.left;
                // return root;
            }else {
                TreeNode minRight = cur.right;
                TreeNode minRightParent = cur;
                while (minRight.left != null) {
                    minRightParent = minRight;
                    minRight = minRight.left;
                }
                if (minRightParent.val == cur.val){
                    cur.right = minRight.right;
                }else {
                    minRightParent.left = minRight.right;
                }
                minRight.left = cur.left;
                minRight.right = cur.right;
                cur = minRight;
            }
        }
        if (curParent == null) {
            return cur;
        }else {
            if (curParent.left != null && curParent.left.val == key) {
                curParent.left = cur;
            }else {
                curParent.right = cur;
            }
        }
        return root;
    }
}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

方法二 递归

1 方法思想

2 代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
                //后序遍历
        if (root == null) return root;

        if (root.val > key){
            root.left = deleteNode(root.left, key);
            return root;
        }
        if (root.val < key){
            root.right = deleteNode(root.right , key);
            return root;
        }
        if (root.val == key){
            if (root.left == null && root.right == null) return null;
            if (root.left == null) return root.right;
            if (root.right == null) return root.left;
            TreeNode minRight = root.right;
            while (minRight.left != null){
                minRight = minRight.left;
            }
            root.right = deleteNode(root.right, minRight.val);
            minRight.right = root.right;
            minRight.left = root.left;
            return minRight;
        }
        return root;
    }
}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值