VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-of-nodes-in-a-complete-binary-tree/

⇱ Count number of nodes in a complete Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count number of nodes in a complete Binary Tree

Last Updated : 23 Jul, 2025

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:

πŸ‘ Image

Output: 7

Input:

πŸ‘ Image

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.


Output
9

Time Complexity: O(N) as in traversal all the nodes are visited

Reason:

  • Recurrence Relation: T(N) = 2*T(N/2) + O(1) 
  • Solve via Master's Theorem, you will get O(N) as Time.


Auxiliary Space: O(h) = O(log N)  as height of CBT is LogN

Reason:

  • Recursion Stack Space (which takes elements up to the height of the tree)
  • As you will go left, left till the corner leaf node. All of these stay in the stack on top of one another.

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 : 


πŸ‘ Image
Tree

1) Lets take 2 variables , current = 15 and prev = NULL and count  = 0 .

πŸ‘ Image
Tree

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

πŸ‘ Image
Tree

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

πŸ‘ Image
Tree

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

πŸ‘ Image
Tree

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)

πŸ‘ Image
Tree

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 

πŸ‘ Image
Tree

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

πŸ‘ Image
Tree

9)Now current -> left is NULL, increment count and moves current to current -> right

Current  = 15 

Count = 4

πŸ‘ Image

10)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

πŸ‘ Image
Tree

11)Now current -> left is NULL, increment count and moves current to current -> right

Current  = 20

Count = 6

πŸ‘ Image
Tree

12)Now current -> left is NULL, increment count and moves current to current -> right

Current  = 82

Count = 7

πŸ‘ Image
Tree

13)Now current -> left is NULL, increment count and moves current to current -> right

Current  = 122

Count = 8

πŸ‘ Image
Tree

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.



Output
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:

  • Define a functionleft_height(root) and find the left height of the given Tree by traversing in the root's left direction and store it in a variable, say leftHeight.
  • Define a function right_height(root) and find the right height of the given Tree by traversing in the root's right direction and store it in a variable, say rightHeight.
  • Find the left and the right height of the given Tree for the current root value and if it is equal then return the value of (2height - 1) as the resultant count of nodes.
  • Otherwise, recursively call for the function for the left and right sub-trees and return the sum of them + 1 as the resultant count of nodes.

Below is the implementation of the above approach.


Output
9

Time Complexity: O((log N)2)

  • Calculating the height of a tree with x nodes takes (log x) time.
  • Here, we are traversing through the height of the tree.
  • For each node, height calculation takes logarithmic time.
  • As the number of nodes is N, we are traversing log(N) nodes and calculating the height for each of them.
  • So the total complexity is (log N * log N) = (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)

Comment