Judgment of binary sort tree (hackerrank) java

A brief introduction to the binary tree sorting tree:

  1. The value of every node in a node’s left subtree is less than the data value of that node.
  2. The value of every node in a node’s right subtree is greater than the data value of that node.

    I saw a lot of people writing on the Internet before, just want to say have you tested it? How many cases have you tested?

    Let’s start writing the source code of my score test on Hackerrank:

boolean checkBST(Node root) {
    return checkBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE); 
}

boolean checkBST(Node root, int min, int max) {
    if (root == null) return true; 

    if (root.data <= min || root.data >= max)
        return false;

    if (!checkBST(root.left, min, root.data) || !checkBST(root.right, root.data, max))
        return false;

    return true;
}

Overloading is to support more ways to use, and it is still the second method at all.

Guess you like

Origin blog.csdn.net/u014377853/article/details/52672797