
- 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 find number of nodes in a range in Python
Suppose we have a BST, and we also have left and right bounds l and r, we have to find the count of all nodes in root whose values are present in between l and r (inclusive).
So, if the input is like
l = 7, r = 13, then the output will be 3, as there are three nodes: 8, 10, 12.
To solve this, we will follow these steps−
stack := a stack and insert root at first, count := 0
while stack is not empty, do
node := top element of stack, and pop element
if node is not null, then
if l <= data of node <= r, then
count := count + 1
stack := push right of node and left of node into stack
otherwise when data of node < l, then
stack := push right of node into stack
otherwise,
stack := push left of node into stack
return count
Example
from collections import deque 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, l, r): stack, count = [root], 0 while stack: node = stack.pop() if node: if l <= node.data <= r: count += 1 stack += [node.right, node.left] elif node.data < l: stack += [node.right] else: stack += [node.left] return count ob = Solution() root = TreeNode(12) root.left = TreeNode(8) root.right = TreeNode(15) root.left.left = TreeNode(3) root.left.right = TreeNode(10) print(ob.solve(root, 7,13))
Input
root = TreeNode(12) root.left = TreeNode(8) root.right = TreeNode(15) root.left.left = TreeNode(3) root.left.right = TreeNode(10) 7,13
Output
3
- Related Articles
- Program to find number of good leaf nodes pairs using Python
- Program to find out the number of special numbers in a given range in Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to count number of BST with n nodes in Python
- Program to remove all nodes from BST which are not in range in Python
- Python Program to Find the Sum of all Nodes in a Tree
- Program to find bitwise AND of range of numbers in given range in Python
- Program to find number of nodes in the sub-tree with the same label using Python
- Program to find number of possible BSTs can be generated using n distinct nodes in Python
- Program to Find Out the Special Nodes in a Tree in Python
- Program to find longest path between two nodes of a tree in Python
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Program to find maximum sum of non-adjacent nodes of a tree in Python

Advertisements