check if a tree is a binary search tree

前端 未结 9 894
遥遥无期
遥遥无期 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:53

    boolean bst = isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
    
    public boolean isBST(Node root, int min, int max) {
        if(root == null) 
            return true;
    
        return (root.data > min &&
                root.data < max &&
                isBST(root.left, min, root.data) &&
                isBST(root.right, root.data, max));
        }
    

提交回复
热议问题