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 →

  • sayanarijit 4 years ago+ 0 comments

    My code is:

    def isBST(root):
        if root.left and root.left.data >= root.data:
            return False
        if root.left:
            print(root.left.data,"<",root.data)
        if root.right and root.right.data <= root.data:
            return False
        if root.right:
            print(root.right.data,">",root.data)
        return True
    
    def checkBST(root):
        if not isBST(root):
            return False
        if root.left:
            checkBST(root.left)
        if root.right:
            checkBST(root.right)
        return True
    

    Input:

    2
    1 2 4 3 5 6 7
    

    Output:

    2 < 3
    6 > 3
    1 < 2
    4 > 2
    5 < 6
    7 > 6
    Yes
    

    Can anyone please where is the problem?

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