Leetcode brushes the 13th day of the binary tree series "98 BST and its verification"

98 BST and its verification

BST is a binary search tree, a binary search tree, which is also called a binary sort tree. When it is traversed in the middle order, an increasing sequence can be obtained.

 

Title description

Given a binary tree, judge whether it is a valid binary search tree

Suppose a binary search tree has the following characteristics:

The left subtree of the node only contains numbers less than the current node.

The right subtree of the node only contains numbers greater than the current node.

All left and right subtrees must themselves be binary search trees.

 

Example

enter:

2
/
1 3

Output: true

enter:

​ 5

/ \

1 4

​ / \

​ 3 6

Output: false

Explanation: The value of the root node is 5, but the value of the right child node is 4, which does not satisfy the condition that the value of all nodes in the right subtree is greater than the root node

 

Problem analysis

   According to the depth-first search suggested by the title, you can write it in a recursive way. Each time it is judged whether the left and right subtrees meet the conditions of the binary search tree;

The value of all nodes of the left subtree is less than the value of the root node

The value of all nodes in the right subtree is greater than the value of the root node

  But here is not just to compare the values ​​of the root node and the left and right nodes to meet the conditions, it is to meet the conditions on the entire binary tree, so there should be a boundary range for the value of each node, for example:

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-hCymzVOj-1601715451019) (D:\My Blog\Brush the title\img\leetCode98 icon 1.png) ]

  Therefore, when returning false when recursing, you can't just compare the size of the left and right nodes and the parent node. How to do it is understood after reading the code of the big guy. The code is as follows:

public boolean isBST(TreeNode root, long min, long max) {
    
    
    if (root == null) {
    
    
        return true;
    }

    if (root.val <= min || root.val >= max) {
    
    
        return false;
    }
    return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

public boolean isValidBST(TreeNode root) {
    
    
    if (root == null) {
    
    
        return true;
    }
    return isBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

 

The realization idea is as follows:

  1. First use a maximum value and a minimum value as the boundary judgment condition to ensure that the root node value of the entire tree is within this range. The maximum value of the Long type is used here because I have a test case of int when the code is submitted. The maximum value of the type, causing an error.

  2. When the program first enters the isBST function, for

    root.val <= min || root.val >= max
    

    The bool value of this condition is true. Then enter the following recursive condition.

  3. And this recursion is also set very concisely. For the nodes of the left subtree, the minimum value of the boundary remains unchanged, while the maximum value is set to the value of the current node, that is, the node of the left subtree cannot be greater than the value of the current node; For the nodes of the right subtree, the maximum value of the boundary has not changed, while the minimum value is set to the value of the current node, that is, the node of the right subtree cannot be smaller than the value of the current node.

  4. The judgment condition judges whether the range of each node is in (min, max), and returns false if it exceeds the range, and the constraints of the boundary conditions are given by parameter passing during recursion.

     

Program execution result:

BST and its verification
Reference: Xiaohao Algorithm-BST and its verification

Guess you like

Origin blog.csdn.net/weixin_44184990/article/details/108911400