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 →

  • alex_vanoene 3 years ago+ 0 comments

    I first tried to check every node, but I couldn't figure out how to check the entire tree until I read your code, here's the code I tried first:

    def checkBST(root):
        truth = True
        if(root.left != None):
            truth *= root.left.data < root.data
            truth *= checkBST(root.left)
        if(root.right != None):
            truth *= root.data < root.right.data
            truth *= checkBST(root.right)
        return(truth)
    

    It doesn't work because it doesn't check for duplicates or if leafs are actually in order. Thanks for the elegant solution!

    0|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature