java 450.删除BST中的节点(#1).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 450.删除BST中的节点(#1).java相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        
        if(root == null) return null;
        
        if(key < root.val){
            root.left = deleteNode(root.left, key);
        } else if (key > root.val){
            root.right = deleteNode(root.right, key);
        } else {
            root = deleteRoot(root);
        }
        
        return root;
    }
    public TreeNode deleteRoot(TreeNode root){
        if(root == null) return null;
        if(root.right == null) return root.left;
        TreeNode temp = root.right;// root.right should be the new root
        while(temp.left != null){
            temp = temp.left;// find the left-most node
        }
        temp.left = root.left;
        return root.right;
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    /*
    如果被删除节点有左右孩子,把左孩子的最右节点或者右孩子最左节点替换当前节点
    其他情况直接把后续节点补上去
    */
    public TreeNode deleteNode(TreeNode root, int key) {
        TreeNode pre = new TreeNode(-1);
        TreeNode rt = pre;
        pre.right = root;
        while(root!=null){
            if(key>root.val){
                pre = root;
                root = root.right;
            }else if(key<root.val){
                pre = root;
                root = root.left;
            }else{
                if(root.left==null && root.right==null){
                    //被删除节点是叶子节点,直接删掉
                    if(pre.right==root) pre.right=null;
                    else pre.left=null;
                }else if(root.left!=null && root.right==null){
                    //被删除节点没有右孩子,用左孩子替换位置
                    if(pre.right==root) pre.right = root.left;
                    else pre.left = root.left;
                }else if(root.right!=null && root.left==null){
                    //被删除节点没有左孩子,用右孩子替换位置
                    if(pre.right==root) pre.right = root.right;
                    else pre.left = root.right;
                }else{
                    //被删除节点既有左孩子又有右孩子
                    //找到右孩子的最左节点,替换被删除节点
                    TreeNode pre2 = root;
                    TreeNode pt = root.right;
                    while(pt.left!=null){
                        pre2 = pt;
                        pt = pt.left; //pt要和root进行替换
                    }
                    root.val = pt.val;
                    if(pre2.left==pt) pre2.left = pt.right;
                    else pre2.right = pt.right;
                    
                }
                return rt.right;
            }
        }
        return rt.right;
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) return root;
        if (root.val > key) {
            root.left = deleteNode(root.left, key);
        } else if (root.val < key) {
            root.right = deleteNode(root.right, key);
        } else {
            if (root.left == null && root.right == null) return null;
            else if (root.left == null) return root.right;
            else if (root.right == null) return root.left;
            else {
                TreeNode pre = root;
                TreeNode cur = root.right;
                while (cur.left != null) {
                    pre = cur;
                    cur = cur.left;
                }
                root.val = cur.val;
                if (pre.left == cur) {
                    pre.left = cur.right;
                } else {
                    pre.right = cur.right;
                }
                //root.right = deleteNode(root.right, cur.val);
            }
        }
        return root;
    }
}

以上是关于java 450.删除BST中的节点(#1).java的主要内容,如果未能解决你的问题,请参考以下文章

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java

450. Delete Node in a BST 删除bst中的一个节点

[LeetCode] 450. Delete Node in a BST