We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Tutorials
  3. Cracking the Coding Interview
  4. Trees: Is This a Binary Search Tree?
  5. Discussions

Trees: Is This a Binary Search Tree?

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • robertram 4 years ago+ 0 comments

    I think it's much simpler to just add the range check to the return condition. In that way you do not need to negate it and is explicit what it is doing.

    boolean checkBST(Node root) {
            return checkBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
        }
        boolean checkBST(Node node, int min, int max) {
            if (node == null) return true;
            return  min < node.data && node.data < max && 
                checkBST(node.left, min, node.data) && 
                checkBST(node.right, node.data, max);
        }
    
    33|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature