Search In BST
Given a BST and an integer k. Find if the integer k is present in the given BST or not. You have to return true, if a node with data k is present, return false otherwise.
Note: Assume that BST contains all unique elements.
Input Format:
Output Format:
Constraints:
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import queue | |
class BinaryTreeNode: | |
def __init__(self, data): | |
self.data = data | |
self.left = None | |
self.right = None | |
def searchInBST(root, k): | |
if root is None: | |
return None | |
if k == root.data: | |
return root | |
if k < root.data: | |
return searchInBST(root.left, k) | |
if k > root.data: | |
return searchInBST(root.right, k) | |
def buildLevelTree(levelorder): | |
index = 0 | |
length = len(levelorder) | |
if length<=0 or levelorder[0]==-1: | |
return None | |
root = BinaryTreeNode(levelorder[index]) | |
index += 1 | |
q = queue.Queue() | |
q.put(root) | |
while not q.empty(): | |
currentNode = q.get() | |
leftChild = levelorder[index] | |
index += 1 | |
if leftChild != -1: | |
leftNode = BinaryTreeNode(leftChild) | |
currentNode.left =leftNode | |
q.put(leftNode) | |
rightChild = levelorder[index] | |
index += 1 | |
if rightChild != -1: | |
rightNode = BinaryTreeNode(rightChild) | |
currentNode.right =rightNode | |
q.put(rightNode) | |
return root | |
# Main | |
levelOrder = [int(i) for i in input().strip().split()] | |
root = buildLevelTree(levelOrder) | |
k=int(input()) | |
node=searchInBST(root, k) | |
if node: | |
print("true") | |
else: | |
print("false") |
Comments
Post a Comment
Please give us your valuable feedback