The twenty-second day of the code random recording algorithm training camp|235. The nearest common ancestor of the binary search tree, 701. The insertion operation in the binary search tree, 450. Delete the node in the binary search tree

Table of contents

235. Nearest Common Ancestor of Binary Search Tree

1. Recursive implementation

2. Implementation of iterative method

 701. Insertion operation in binary search tree (recursive implementation)

450. Delete nodes in the binary search tree (recursive implementation)


235. Nearest Common Ancestor of Binary Search Tree

Compared with the nearest common ancestor of the binary tree, this question is simpler, because the characteristics of the binary search tree can be used.

Topic link/article explanation: code caprice

Solution ideas:

1. Recursive implementation

The principles of the iterative method and the recursive method are the same. In fact, I think the essence of this iterative method is a recursive method, but the steps are different! ! !

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while(root != null){
            if(root.val > p.val && root.val > q.val){
                TreeNode left = lowestCommonAncestor(root.left,p,q);
                if(left != null) return left;
            }else if(root.val < p.val && root.val < q.val){
                TreeNode right = lowestCommonAncestor(root.right,p,q);
                if(right != null) return right;
            }else{
                return root;
            }
        }
        return null;
    }
}

2. Implementation of iterative method

The principles of the iterative method and the recursive method are the same. In fact, I think the essence of this iterative method is a recursive method, but the steps are different! ! !

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while(root != null){
            if(root.val > p.val && root.val > q.val){
                TreeNode left = lowestCommonAncestor(root.left,p,q);
                if(left != null) return left;
            }else if(root.val < p.val && root.val < q.val){
                TreeNode right = lowestCommonAncestor(root.right,p,q);
                if(right != null) return right;
            }else{
                return root;
            }
        }
        return null;
    }
}

 701. Insertion operation in binary search tree (recursive implementation)

This question is simpler than imagined. You can think about how to do it yourself, and then watch the video explanation, and you will find why this question is relatively simple.

Topic link/article explanation: code caprice

Solution ideas:

Listen to Ka Ge's video explanation more! ! ! The insertion operation in a binary tree is to construct a binary tree. We must be able to find the node we want to insert in the leaf node. Then we can compare the val value of the current node with the given val according to the characteristics of the binary search tree, and directly determine whether it is in its left subtree or not. The right subtree is inserted! ! ! Finally, just return to the root node directly! ! ! Before listening to Ka Ge's explanation, it was just a mess, but the first feeling was very complicated, and there were various insertion methods and so on! ! ! After listening to Ka Ge's video explanation, the thinking is still clear. You must listen more, watch more, and swipe more to open your mind! ! ! Transcoding for non-scientific classes is indeed a bit painful, and it really subverts the way of thinking that has not been trained before! ! ! Come on! ! !

/**
 * 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 insertIntoBST(TreeNode root, int val) {
        //第二步:确定终止条件
        if(root == null) return new TreeNode(val);;

        //第三步:确定单层处理逻辑
        if(root.val > val){
            root.left = insertIntoBST(root.left,val); //直接根据二叉搜索树的特性比较当前节点的val值和给定的val,直接判断在其左子树还是右子树进行插入操作
        }else if(root.val < val){
            root.right = insertIntoBST(root.right,val); //直接根据二叉搜索树的特性比较当前节点的val值和给定的val,直接判断在其左子树还是右子树进行插入操作
        }

        return root;
    }

    
}

作者:vansven-h
链接:https://leetcode.cn/problems/insert-into-a-binary-search-tree/solution/701er-cha-sou-suo-shu-zhong-de-cha-ru-ca-fj39/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

450. Delete nodes in the binary search tree (recursive implementation)

Compared with the insertion operation, this question is more difficult, and it involves changing the structure of the tree

Topic link/article explanation: code caprice

Solution ideas:

The main reason is that there are five different situations for deleting nodes, which need to be dealt with one by one according to different situations. For the specific five situations, see the commented code below! ! ! What I want to say is that once you swipe, you need to watch more, listen to Kage’s videos, and swipe more. Only by mastering the methodology can you have the capital to build your own wheels. These are basic algorithms, and you don’t know how to talk about it. Advanced knowledge! ! !

/**
 * 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 null; //第一种情况:没找到要删除的节点,直接返回null值即可
        else if(root.val == key){     //以下四种情况都是在找到要删除节点的情况下进行讨论的
            if(root.left == null && root.right == null) return null; //第二种情况:删除的时叶子节点
            else if(root.left != null && root.right == null) return root.left; //第三种情况:删除左不为空,右为空的节点
            else if(root.left == null && root.right != null) return root.right;//第四种情况:删除左为空,右不为空的节点
            else{
                TreeNode current = root.right;  //第五种情况:删除左不为空,右不为空的节点,根据二叉搜索树的特性,需要一直搜索右子树的左节点,一直其叶节点进行插入
                while( current.left != null){
                    current = current.left;
                }
                current.left = root.left;
                return root.right;
            }
        }

        //第三步:确定单层处理逻辑,根据二叉树的搜索特性,直接比较当前遍历节点的val值和key值,直接判断在其左子树还是右子树进行删除节点操作
        if(root.val > key) root.left = deleteNode(root.left,key);
        else if(root.val < key) root.right = deleteNode(root.right,key);

        return root;
    }
}

Guess you like

Origin blog.csdn.net/tore007/article/details/130628721