Remove Node in Binary Search Tree 解答

从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论。

这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况。

一种简单些的思维是只考虑删除节点是否有右子树(因为第一个比删除节点大的节点可能出现在右子树,不会出现在左子树)。

这里用Target表示删除节点,Parent表示删除节点的母节点。

1. 没有右子树

Parent.left / Parent.right = Target.left

2. 有右子树

1) 找到第一个比删除节点大的节点Node

2) 删除该节点Node

3) Target.val = Node.val 或 保留 Node 删除Target

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of the binary search tree.
     * @param value: Remove the node with given value.
     * @return: The root of the binary search tree after removal.
     */
    public TreeNode removeNode(TreeNode root, int value) {
        // write your code here
        TreeNode dummy = new TreeNode(0);
        dummy.left = root;
        TreeNode parent = findNodeParent(dummy, root, value);
        TreeNode target;
        if (parent.left != null && parent.left.val == value) {
            target = parent.left;
        } else if (parent.right != null && parent.right.val == value) {
            target = parent.right;
        } else {
            return dummy.left;
        }
        deleteNode(parent, target);
        return dummy.left;
    }

    private TreeNode findNodeParent(TreeNode parent, TreeNode node, int val) {
        if (node == null || node.val == val) {
            return parent;
        }
        if (node.val < val) {
            return findNodeParent(node, node.right, val);
        } else {
            return findNodeParent(node, node.left, val);
        }
    }

    private void deleteNode(TreeNode parent, TreeNode target) {
        if (target.right == null) {
            if (parent.right == target) {
                parent.right = target.left;
            } else {
                parent.left = target.left;
            }
        } else {
            // Find first element that is greater than target
            TreeNode node1 = target.right;
            TreeNode node2 = target;
            while (node1.left != null) {
                node2 = node1;
                node1 = node1.left;
            }
            // Remove node1
            if (node1 == node2.left) {
                node2.left = node1.right;
            } else {
                node2.right = node1.right;
            }
            // Remove target
            if (target == parent.right) {
                parent.right = node1;
            } else {
                parent.left = node1;
            }
            node1.left = target.left;
            node1.right = target.right;
        }
    }
}
时间: 2024-10-12 04:04:27

Remove Node in Binary Search Tree 解答的相关文章

[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still

【Lintcode】087.Remove Node in Binary Search Tree

题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after rem

Remove Node in Binary Search Tree

Question:  Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree a

[Algorithm] Delete a node from Binary Search Tree

The solution for the problem can be divided into three cases: case 1: if the delete node is leaf node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina

Validate Binary Search Tree 解答

Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

[Lintcode] Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow, after Insert node 6, the tree

Binary Search Tree Iterator 解答

Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod