
- 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
Find median of BST in O(n) time and O(1) space in Python
Suppose we have Binary Search Tree(BST), we have to find median of it. We know for even number of nodes, median = ((n/2th node + (n+1)/2th node) /2 For odd number of nodes, median = (n+1)/2th node.
So, if the input is like
then the output will be 7
To solve this, we will follow these steps −
if root is same as None, then
return 0
node_count := number of nodes in the tree
count_curr := 0
current := root
while current is not null, do
if current.left null, then
count_curr := count_curr + 1
if node_count mod 2 is not 0 and count_curr is same as(node_count + 1) /2, then
return previous.data
otherwise when node_count mod 2 is 0 and count_curr is same as(node_count/2) +1, then
return(previous.data + current.data) /2
previous := current
current := current.right
otherwise,
previous := current.left
while previous.right is not null and previous.right is not same as current, do
previous := previous.right
if previous.right is null, then
previous.right := current
current := current.left
otherwise,
previous.right := None
previous := previous
count_curr := count_curr + 1
if node_count mod 2 is not 0 and count_curr is same as(node_count + 1) / 2, then
return current.data
otherwise when node_count mod 2 is 0 and count_curr is same as(node_count / 2) + 1, then
return(previous.data+current.data) /2
previous := current
current := current.right
Example
Let us see the following implementation to get better understanding −
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def number_of_nodes(root): node_count = 0 if (root == None): return node_count current = root while (current != None): if (current.left == None): node_count+=1 current = current.right else: previous = current.left while (previous.right != None and previous.right != current): previous = previous.right if(previous.right == None): previous.right = current current = current.left else: previous.right = None node_count += 1 current = current.right return node_count def calculate_median(root): if (root == None): return 0 node_count = number_of_nodes(root) count_curr = 0 current = root while (current != None): if (current.left == None): count_curr += 1 if (node_count % 2 != 0 and count_curr == (node_count + 1)//2): return previous.data elif (node_count % 2 == 0 and count_curr == (node_count//2)+1): return (previous.data + current.data)//2 previous = current current = current.right else: previous = current.left while (previous.right != None and previous.right != current): previous = previous.right if (previous.right == None): previous.right = current current = current.left else: previous.right = None previous = previous count_curr+= 1 if (node_count % 2 != 0 and count_curr == (node_count + 1) // 2 ): return current.data elif (node_count%2 == 0 and count_curr == (node_count // 2) + 1): return (previous.data+current.data)//2 previous = current current = current.right root = TreeNode(7) root.left = TreeNode(4) root.right = TreeNode(9) root.left.left = TreeNode(2) root.left.right = TreeNode(5) root.right.left = TreeNode(8) root.right.right = TreeNode(10) print(calculate_median(root))
Input
root = TreeNode(7) root.left = TreeNode(4) root.right = TreeNode(9) root.left.left = TreeNode(2) root.left.right = TreeNode(5) root.right.left = TreeNode(8) root.right.right = TreeNode(10)
Output
7
- Related Articles
- Find median of BST in O(n) time and O(1) space in C++
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Print left rotation of array in O(n) time and O(1) space in C Program.
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Print BST keys in given Range - O(1) Space in C++
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Check for balanced parentheses in an expression - O(1) space - O(N^2) time complexity in C++
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Find duplicates in constant array with elements 0 to N-1 in O(1) space in C++
- Print n x n spiral matrix using O(1) extra space in C Program.
