
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to check whether one value is present in BST or not in Python
Suppose we have a binary search tree and another input called val, we have to check whether val is present in the tree or not.
So, if the input is like
val = 7, then the output will be True, as 7 is present in the tree.
To solve this, we will follow these steps−
Define a function solve() . This will take root, val
if root is null, then
return False
if data of root is same as val, then
return True
if data of root < val, then
return solve(left of root, val)
return solve(right of root, val)
Let us see the following implementation to get better understanding−
Example
class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right class Solution: def solve(self, root, val): if not root: return False if root.data == val: return True if root.data > val: return self.solve(root.left, val) return self.solve(root.right, val) ob = Solution() root = TreeNode(5) root.left = TreeNode(1) root.right = TreeNode(9) root.right.left = TreeNode(7) root.right.right = TreeNode(10) root.right.left.left = TreeNode(6) root.right.left.right = TreeNode(8) print(ob.solve(root, 7))
Input
root = TreeNode(5) root.left = TreeNode(1) root.right = TreeNode(9) root.right.left = TreeNode(7) root.right.right = TreeNode(10) root.right.left.left = TreeNode(6) root.right.left.right = TreeNode(8) 7
Output
True
- Related Articles
- Program to check whether a binary tree is BST or not in Python
- Program to check whether one tree is subtree of other or not in Python
- Program to check all 1s are present one after another or not in Python
- Program to check whether one point can be converted to another or not in Python
- Program to check whether given graph is bipartite or not in Python
- Check whether the Average Character of the String is present or not in Python
- Program to check whether every one has at least a friend or not in Python
- Program to check whether each node value except leaves is sum of its children value or not in Python
- Python program to check whether a list is empty or not?
- Program to check whether given matrix is Toeplitz Matrix or not in Python
- Program to check whether a binary tree is complete or not in Python
- Program to check whether given number is Narcissistic number or not in Python
- Program to check whether given tree is symmetric tree or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check whether given list is in valid state or not in Python

Advertisements