Day30——Binary tree topic


22. Verify binary search tree

link

Given a binary tree, determine whether it is a valid binary search tree.

Suppose a binary search tree has the following characteristics:

  • A node's left subtree contains only numbers less than the current node.
  • The right subtree of a node contains only numbers greater than the current node.
  • All left and right subtrees must themselves be binary search trees.

The in-order traversal of a binary search tree is an ordered array. You can use this feature to determine whether it is a binary search tree, in-order traversal, and verify whether the traversed elements are from small to large.

Code implementation :

class Solution {
    
    
    private long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
    
    
        if(root==null){
    
    
            return true;
        }
        boolean left = isValidBST(root.left);
        if(root.val<=prev){
    
    
            return false;
        }
        prev = root.val;
        boolean right = isValidBST(root.right);
        return right&&left;
    }
}

23. Minimum absolute difference of binary search tree

link

In-order traversal : After in-order traversal is an ordered array

image-20221106185406263

Double pointer method implementation code

class Solution {
    
    
    TreeNode pre = null;
    int result = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
    
    
        if(root==null) return 0;
        dfs(root);
        return result;
    }
    public void dfs(TreeNode root){
    
    
        if(root==null) return;
        dfs(root.left);
        if(pre!=null){
    
    
            result = Math.min(result,root.val-pre.val);
        }
        pre = root;
        dfs(root.right);
    }
}

24. Insertion operation in binary search tree

link

Recursive Trilogy:

  • Determine recursive function parameters and return values

  • Determine the termination condition

The termination condition is that when the traversed node is found to be null, it is the position where the node is to be inserted, and the inserted node is returned.

  • Determine the logic for a single level of recursion

Code implementation :

recursive method

class Solution {
    
    
    public TreeNode insertIntoBST(TreeNode root, int val) {
    
    
        if(root==null) return new TreeNode(val);
        if(root.val<val) root.right = insertIntoBST(root.right,val);
        if(root.val>val) root.left = insertIntoBST(root.left,val);
        return root;
    }
}

Iterative method

class Solution {
    
    
    public TreeNode insertIntoBST(TreeNode root, int val) {
    
    
        if (root == null) return new TreeNode(val);
        TreeNode newRoot = root;
        TreeNode pre = root;
        while (root != null) {
    
    
            pre = root;
            if (root.val > val) {
    
    
                root = root.left;
            } else if (root.val < val) {
    
    
                root = root.right;
            } 
        }
        if (pre.val > val) {
    
    
            pre.left = new TreeNode(val);
        } else {
    
    
            pre.right = new TreeNode(val);
        }

        return newRoot;
    }
}

Guess you like

Origin blog.csdn.net/weixin_54040016/article/details/127719353