【刷题打卡】day11-二叉树

从现在开始每天至少刷一道题。
题库:lintcode

有些题目链接打不开,需要权限,那大家就去九章算法参考答案里找找。

Insert Node in a Binary Search Tree

题目链接
难度:easy
算法:二分法

解题思路
插入一定在树的叶子节点上。如果target大于节点值,往右子树搜索;否则,往左子树搜索。当搜索null时,说明可以找到可以插入的位置。存储插入位置之前的那个父节点。如果该父节点大于target,target是父节点的左节点,否则是右节点。

时间复杂度:O(logn)
空间复杂度:O(1)

解法

/**
 * 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 node: insert this node into the binary search tree
     * @return: The root of the new binary search tree.
     */
    public TreeNode insertNode(TreeNode root, TreeNode node) {
     
        // write your code here
        if(root == null){
     
            return node;
        }
        
        TreeNode curr = root;
        TreeNode last = null;
        while(curr != null){
     
            last = curr;
            if(curr.val > node.val){
     
                curr = curr.left;
            }else{
     
                curr = curr.right;
            }
        }
        
        if(last != null){
     
            if(last.val > node.val){
     
                last.left = node;
            }else{
     
                last.right = node;
            }
            
        }

        return root;
    }
}

Remove Node in Binary Search Tree

题目链接
难度:hard
算法:二分法

解题思路
先找出target的节点以及其父节点。然后用target左子树的最大值或者右子树的最小值替换target节点。注意,当target节点就是根节点的时候,根节点没有父节点,所以我们要给根节点造一个父节点。
具体步骤

  1. 从根节点出发,如果target value 大于根节点的值,那么往右子树继续搜索。如果小于,往左子树搜索。当等于的时候,停止搜索,返回当前节点的父节点
  2. 假设用target左子树的最大值替换target节点,用右子树的最小值也行,只考虑其中一种方案。如果target的左节点为null,那么parent直接指向target的右节点。否则,在target的左子树查找最大节点及其父节点,也就是在最右边。找到后,将父值指向最大节点的左节点(因为右节点已经是空了),将target替换成最大节点。通过最大节点的左右节点指向target的左右节点。

时间复杂度:O(logn), 查找节点通过二分进行的,O(logn);删除节点也是二分,其实一直往右边。O(logn)
空间复杂度:O(1)

解法

/**
 * 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 = findNode(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;
        }
        
        // System.out.printf("%d %d\n", parent.val, target.val);
        
        //replace target node 
        deleteNode(parent, target);
        
        return dummy.left;
    }
    private TreeNode findNode(TreeNode parent, TreeNode node, int value){
     
        if(node == null){
     
            return parent;
        }
        if(node.val == value){
     
            return parent;
        }
        
        while(node != null){
     
            if(node.val > value){
     
                parent = node;
                node = node.left;
            }else if(node.val < value){
     
                parent = node;
                node = node.right;
            }else{
     
                break;
            }
        }
        return parent;
    }
    
    private void deleteNode(TreeNode parent, TreeNode node){
     
        if(node.left == null){
     
            if(parent.left == node){
     
                parent.left = node.right;
            }else{
     
                parent.right = node.right;
            }
        }
        else{
     
            //replace node with the largest value on the left
            //node 3
            //parent 5
            TreeNode tmp = node.left; //2
            TreeNode father = node; //3
            
            while(tmp.right != null){
     
                father = tmp;
                tmp = tmp.right;
            }
            //remove the largest value 
            if(father.left == tmp){
     
                father.left = tmp.left; //null
            }else{
     
                father.right = tmp.left;
            }
            // System.out.printf("%d %d\n",parent.left.val, node.val);
            if(parent.left == node){
     
                System.out.print(tmp.val);
                parent.left = tmp; //2
            }else{
     
                parent.right = tmp;
            }
            
            tmp.left = node.left; //null
            tmp.right = node.right; //4
        }
    }
    
    
}

你可能感兴趣的:(算法,二分法,算法,二叉树,数据结构)