二叉排序树的判断(hackerrank) java

简单介绍下二叉树排序树:

  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.

    之前在网上看到很多人写的,只想说你们测试过吗?你们测试了几个案例?

    下面开始写我在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;
}

重载是为了支持更多方式使用,根本还是第二个方法。

猜你喜欢

转载自blog.csdn.net/u014377853/article/details/52672797