450. Delete Node in a BST

来源:互联网 发布:有没有钢琴软件 编辑:程序博客网 时间:2024/06/06 12:11

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

Basically, the deletion can be divided into two stages:

Search for a node to remove.
If the node is found, delete the node.
Note: Time complexity should be O(height of tree).

Example:

root = [5,3,6,2,4,null,7]key = 3    5   / \  3   6 / \   \2   4   7Given key to delete is 3. So we find the node with value 3 and delete it.One valid answer is [5,4,6,2,null,null,7], shown in the following BST.    5   / \  4   6 /     \2       7Another valid answer is [5,2,6,null,4,null,7].    5   / \  2   6   \   \    4   7

Subscribe to see which companies asked this question

思路:

  1. 先找到节点。方法:从定义cur指向根节点,并定义pre指向cur的上一个节点。循环开始比较cur.val和key之间的大小,如果等于key,就跳出循环。 如果大于就cur = cur.left;如果小于就cur=cur.right;
    处理细节:情况一:pre==null,情况二:cur==null,情况三:cur是pre的左子树或者是右子树,分两种情况对待。

  2. 进行删除节点,方法:要删除的节点为root,将root的最低层的左孩子节点的值赋值到root中。然后让最底层左孩子节点的根节点指向null。
    难点在于情况分类:情况一:root左右节点为空;情况二:root的左右节点其中一个为空。情况三:左右都不为空,就使用的是上面的常规方法。

代码:

非递归版:

 public TreeNode deleteNode1(TreeNode root ,int key){        TreeNode cur = root,pre = null;        while(cur!=null){            if(cur.val == key) break;            pre = cur;            if(cur.val > key){                cur = cur.left;            }else{                cur = cur.right;            }        }        if(cur==null) return root;        if(pre==null) return del(cur);        if(pre.left!=null && pre.left.val == key){            pre.left = del(cur);        }else{            pre.right = del(cur);        }        return root;    }    public TreeNode del(TreeNode root){        if(root.left==null && root.right==null) return null;        if(root.left==null || null ==root.right){            return (root.left==null)?root.right:root.left;        }        TreeNode pre = root,cur = root.right;//      while(cur!=null){//这样写有问题。下面的pre可能为空。会报空指针异常//          pre = cur;//          cur = cur.left;//      }        while(cur.left !=null){            pre = cur;            cur = cur.left;        }//      进行赋值        root.val = cur.val;        if(pre == root){            root.right = cur.right;//写的好        }else{            pre.left = cur.right;        }        return root;    }

递归版:

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