![]() |
VOOZH | about |
Given the root of a Complete Binary Tree consisting of N nodes, the task is to find the total number of nodes in the given Binary Tree.
Examples:
Input:
Output: 7
Input:
Output: 5
Native Approach: The simple approach to solving the given tree is to perform the DFS Traversal on the given tree and count the number of nodes in it. After traversal, print the total count of nodes obtained.
9
Time Complexity: O(N) as in traversal all the nodes are visited
Reason:
Auxiliary Space: O(h) = O(log N) as height of CBT is LogN
Reason:
Better Approach :
We know how to traverse the tree using recursion or using data structures like stack and queue. But the problem is these data structures consume O(n) space (Why O(n):- Because we need to traverse all the elements so they need to be required to be stored in specific data structures). Recursive functions use something called βthe call stackβ which consumes O(n) space. You can learn even more about time complexity by clicking here
So now the only option left with us that is to think of doing changes to the links. Let's see how to accomplish this task in O(1) space (constant space).
1)Make a pointer which points to current node
2)Continue steps above till the current node pointer is not null
3)If the left of the pointer is NULL , then increment count and move to right.
4)If left pointer is not null , make another temporary pointer to one left of current pointer and move towards right till
it's not null
Dry Run:
Let's take an example and find the number of nodes
Example :
1) Lets take 2 variables , current = 15 and prev = NULL and count = 0 .
2) We should continue the following process until the current node is NULL
3) If Current -> left != NULL , prev = 10
Now iterate prev to right until prev -> right != NULL && prev -> right != current
so now prev = 12
Now prev -> right = NULL, therefore just make a temporary link to current i.e make a temporary right link from 12 to 15 , and move current to left
Current = 10
4) Again repeat step 3) , Now prev = 8 and its right is NULL, so again make a temporary right link from 8 to 10 and move current to left
Current = 8
5)Again repeat step 3) , Now prev = 2 and its right is NULL, so again make a temporary right link from 2 to 8 and move current to left
Current = 2
6)Now current -> left == NULL, So increment count and move current to its right
count = 1
current = 8 (We have a temporary pointer so we are able to go back)
7)Again repeat step 3) , Now prev = 2 and its right is NULL, now when we iterate in loop prev -> right != curr, we stop when prev -> right = NULL
i.e prev = 2 , so make prev -> right = NULL and increment the count,move current to current -> right
The temporary link from 2 -> 8 is removed
Current = 10
Count = 2
8)Again repeat step 3) , Now prev = 8 and its right is NULL, now when we iterate in loop prev -> right != curr, we stop when prev -> right = NULL
i.e prev = 8 , so make prev -> right = NULL and increment the count,move current to current -> right
The temporary link from 8 -> 10 is removed
Current = 12
Count = 3
9)Now current -> left is NULL, increment count and moves current to current -> right
Current = 15
Count = 4
π Image10)Again repeat step 3), Now prev = 10 and its right is NULL, now when we iterate in loop prev -> right != curr, we stop when prev -> right = NULL
i.e prev = 12 , so make prev -> right = NULL and increment the count,move current to current -> right
The temporary link from 12 -> 15 is removed
Current = 20
Count = 5
11)Now current -> left is NULL, increment count and moves current to current -> right
Current = 20
Count = 6
12)Now current -> left is NULL, increment count and moves current to current -> right
Current = 82
Count = 7
13)Now current -> left is NULL, increment count and moves current to current -> right
Current = 122
Count = 8
14)Now the current is NULL, so stop the loop .
In this way, we have found the count of nodes in a binary search tree in O(1) space.
Count of nodes in O(1) space : 8
Time Complexity: O(n) ( We visit each node at most once, so the time complexity is an order of n i.e n)
Space complexity: O(1) (We just use some temporary pointer variables to make changes in links, no additional data structure or recursion is used)
Efficient Approach: The above approach can also be optimized by the fact that:
A complete binary tree can have at most (2h + 1 - 1) nodes in total where h is the height of the tree (This happens when all the levels are completely filled).
By this logic, in the first case, compare the left sub-tree height with the right sub-tree height. If they are equal it is a full tree, then the answer will be 2^height - 1. Otherwise, If they aren't equal, recursively call for the left sub-tree and the right sub-tree to count the number of nodes. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
9
Time Complexity: O((log N)2)
Auxiliary Space: O(log N) because of Recursion Stack Space (which takes elements upto the maximum depth of a node in the tree)