
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to remove all nodes from BST which are not in range in Python
Suppose we have a BST, an two values low and high, we have to delete all nodes that are not between [low, high] (inclusive).
So, if the input is like
low = 7 high = 10, then the output will be
To solve this, we will follow these steps −
- Define a function solve() . This will take root, low, high
- if root is null, then
- return
- if low > data of root, then
- return solve(right of root, low, high)
- if high < data of root, then
- return solve(left of root, low, high)
- right of root := solve(right of root, low, high)
- left of root := solve(left of root, low, high)
- return root
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 def print_tree(root): if root is not None: print_tree(root.left) print(root.data, end = ', ') print_tree(root.right) class Solution: def solve(self, root, low, high): if not root: return if low > root.data: return self.solve(root.right,low,high) if high < root.data: return self.solve(root.left,low,high) root.right = self.solve(root.right,low,high) root.left = self.solve(root.left,low,high) return root 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) low = 7 high = 10 ret = ob.solve(root, low, high) print_tree(ret)
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) low = 7 high = 10
Output
7, 8, 9, 10,
- Related Articles
- Program to remove all nodes with only one child from a binary tree in Python?
- Program to count number of BST with n nodes in Python
- Count BST nodes that lie in a given range in C++
- Program to remove all islands which are completely surrounded by water in Python
- How to remove all child nodes from a parent in jQuery?
- Program to find number of nodes in a range in Python
- Program to remove all nodes of a linked list whose value is same as in Python
- Program to make almost BST to exact BST in python
- How to remove all child nodes from a parent using jQuery?
- C++ program to find the shortest distance between two nodes in BST
- Python Program to Find All Nodes Reachable from a Node using BFS in a Graph
- Program to check whether a binary tree is BST or not in Python
- Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
- Program to check whether one value is present in BST or not in Python
- How to remove all child nodes from a parent node using jQuery?

Advertisements