Assume you are given a binary search tree. Check that the binary search tree is “legal”. A binary search tree is legal if it obeys all the properties of the binary search tree. They are the following: All the nodes on the left is smaller than the root. All the nodes on the right is larger than the root. Both the right and the left subtrees are also legal binary search trees. Input Format Input: root = [6,4,9] The list represents how the tree looks like. This will mean: 6 / \ 4 9 This will be true Input: root = [6,4,9,1,None,7,10] The list represents how the tree looks like. This will mean: 6 / \ 4 9 / \ / \ 1 N 7 10 This will be true Input: root = [6,4,9,1,None,7,2] The list represents how the tree looks like. This will mean: 6 / \ 4 9 / \ / \ 1 N 7 2 This will be false Constraints Use the following starter code: from sys import stdin class Node: def __init__(self, data): self.left = None self.right = None self.data = data def set_child(temp_list,index): if index >= len(temp_list) or temp_list[index] == None: return None temp = Node(temp_list[index]) temp.left = set_child(temp_list, index*2+1) temp.right = set_child(temp_list, index*2+2) return (temp) def isValidBST(root): #todo if __name__ == '__main__': flag = True for line in stdin: nums = list(line[1:-1].strip().split(",")) nums = [int(item) if item.isnumeric() else None for item in nums] root = set_child(nums, 0) print(isValidBST(root)) Output Format Output: true True if it is a BST. Else if it is not. Sample Input 0 [6,4,9,1,None,7,2] Sample Output 0 False Sample Input 1 [6,4,9,1,None,7,10] Sample Output 1 True

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Assume you are given a binary search tree. Check that the binary search tree is “legal”. A binary search tree is legal if it obeys all the properties of the binary search tree. They are the following: All the nodes on the left is smaller than the root. All the nodes on the right is larger than the root. Both the right and the left subtrees are also legal binary search trees.

Input Format

Input: root = [6,4,9] The list represents how the tree looks like. This will mean:

  6
 / \
4 9

This will be true

Input: root = [6,4,9,1,None,7,10] The list represents how the tree looks like. This will mean:

      6
      / \
     4 9
    / \ / \
1 N 7 10

This will be true

Input: root = [6,4,9,1,None,7,2] The list represents how the tree looks like. This will mean:

         6
        / \
       4 9
      / \ / \
    1 N 7 2

This will be false

Constraints

Use the following starter code:

from sys import stdin

class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data

def set_child(temp_list,index):
if index >= len(temp_list) or temp_list[index] == None:
return None temp = Node(temp_list[index])
temp.left = set_child(temp_list, index*2+1)
temp.right = set_child(temp_list, index*2+2)
return (temp)

def isValidBST(root):
#todo

if __name__ == '__main__': flag = True for line in stdin:
nums = list(line[1:-1].strip().split(","))
nums = [int(item) if item.isnumeric() else None for item in nums]
root = set_child(nums, 0)
print(isValidBST(root))

Output Format

Output: true

True if it is a BST. Else if it is not.

Sample Input 0

[6,4,9,1,None,7,2]

Sample Output 0

False

Sample Input 1

[6,4,9,1,None,7,10]

Sample Output 1

True

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education