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 →

  • Dan_Golding 4 years ago+ 0 comments

    I ended up with pretty much the same code but instead of a closure variable I just passed the variable in. I used a single element list since it's gets passed by reference which allows changes to persist when going back down the call stack:

    def checkBST(root):
        return(check_in_order(root,[-1]))
        
    def check_in_order(root,prev):
        result = True
        if root.left is not None:
            result &= check_in_order(root.left,prev)
        if prev[0] >= root.data:
            return False
        prev[0] = root.data
        if root.right is not None:
            result &= check_in_order(root.right,prev)
        return result
    
    6|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature