check if a tree is a binary search tree

前端 未结 9 912
遥遥无期
遥遥无期 2021-01-30 09:29

I have written the following code to check if a tree is a Binary search tree. Please help me check the code:

Okay! The code is edited now. This simple solution was sugge

9条回答
  •  长情又很酷
    2021-01-30 09:43

        boolean isBST(TreeNode root, int max, int min) {
            if (root == null) {
                return true;
            } else if (root.val >= max || root.val <= min) {
                return false;
            } else {
                return isBST(root.left, root.val, min) && isBST(root.right, max, root.val);
            }
    
        }
    

    an alternative way solving this problem.. similar with your code

提交回复
热议问题