Count number of nodes in a complete Binary Tree

Last Updated : 30 Jun, 2026

Given the root of a Complete Binary Tree, the task is to find the total number of nodes in the given Binary Tree.

Examples:

Input:

im1

Output: 7
Explanation: This complete binary tree consists of total 7 nodes.

Input:

im2

Output: 5
Explanation: This complete binary tree consists of total 5 nodes.

Try It Yourself
redirect icon

[Naive Approach] Using DFS Traversal - O(n) Time and O(logn) Space

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.

C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int val;
    Node* left;
    Node* right;

    Node(int x)
        : val(x), left(nullptr), right(nullptr) {}
};

// Recursively count all nodes in the tree
int countNodes(Node* root) {
    if (root == nullptr)
        return 0;

    return 1 + countNodes(root->left)
             + countNodes(root->right);
}

int main() {

    // Build the sample tree
    Node* root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->left->left = new Node(11);
    root->left->right = new Node(12);

    root->right->left = new Node(14);
    root->right->right = new Node(5);

    cout << countNodes(root);

    return 0;
}
Java
// Java program to count nodes in a binary tree using recursion
import java.util.*;

class Node {
    int val;
    Node left;
    Node right;
    
    Node(int x) {
        val = x;
        left = null;
        right = null;
    }
}

class GfG {
    
    // Recursively count all nodes in the tree
    static int countNodes(Node root) {
        if (root == null)
            return 0;
        
        return 1 + countNodes(root.left) + countNodes(root.right);
    }
    
    public static void main(String[] args) {
        // Build the sample tree
        Node root = new Node(1);
        
        root.left = new Node(2);
        root.right = new Node(3);
        
        root.left.left = new Node(11);
        root.left.right = new Node(12);
        
        root.right.left = new Node(14);
        root.right.right = new Node(5);
        
        System.out.println(countNodes(root));
    }
}
Python
# Python program to count nodes in a binary tree using recursion

class Node:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# Recursively count all nodes in the tree
def countNodes(root):
    if root is None:
        return 0
    
    return 1 + countNodes(root.left) + countNodes(root.right)

# Driver code
if __name__ == "__main__":
    # Build the sample tree
    root = Node(1)
    
    root.left = Node(2)
    root.right = Node(3)
    
    root.left.left = Node(11)
    root.left.right = Node(12)
    
    root.right.left = Node(14)
    root.right.right = Node(5)
    
    print(countNodes(root))
C#
// C# program to count nodes in a binary tree using recursion
using System;

class Node {
    public int val;
    public Node left;
    public Node right;
    
    public Node(int x) {
        val = x;
        left = null;
        right = null;
    }
}

class GfG {
    
    // Recursively count all nodes in the tree
    static int countNodes(Node root) {
        if (root == null)
            return 0;
        
        return 1 + countNodes(root.left) + countNodes(root.right);
    }
    
    static void Main(string[] args) {
        // Build the sample tree
        Node root = new Node(1);
        
        root.left = new Node(2);
        root.right = new Node(3);
        
        root.left.left = new Node(11);
        root.left.right = new Node(12);
        
        root.right.left = new Node(14);
        root.right.right = new Node(5);
        
        Console.WriteLine(countNodes(root));
    }
}
JavaScript
// JavaScript program to count nodes in a binary tree using recursion

class Node {
    constructor(x) {
        this.val = x;
        this.left = null;
        this.right = null;
    }
}

// Recursively count all nodes in the tree
function countNodes(root) {
    if (root === null)
        return 0;
    
    return 1 + countNodes(root.left) + countNodes(root.right);
}

// Driver code
// Build the sample tree
const root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.left.left = new Node(11);
root.left.right = new Node(12);

root.right.left = new Node(14);
root.right.right = new Node(5);

console.log(countNodes(root));

Output
7

[Expected Approach] - Optimized Recursive Approach- O((log N)2)Time and O(log N) Space

The main idea behind this approach is 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 function left_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.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int val;
    Node* left;
    Node* right;

    Node(int x)
        : val(x), left(nullptr), right(nullptr) {}
};

// Height obtained by moving only to the left
int leftHeight(Node* root) {
    int h = 0;

    while (root) {
        h++;
        root = root->left;
    }

    return h;
}

// Height obtained by moving only to the right
int rightHeight(Node* root) {
    int h = 0;

    while (root) {
        h++;
        root = root->right;
    }

    return h;
}

// Count nodes using complete binary tree property
int countNodes(Node* root) {

    if (root == nullptr)
        return 0;

    int lh = leftHeight(root);
    int rh = rightHeight(root);

    // Perfect binary tree
    if (lh == rh)
        return (1 << lh) - 1;

    return 1 + countNodes(root->left)
             + countNodes(root->right);
}

int main() {

    Node* root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->left->left = new Node(11);
    root->left->right = new Node(12);

    root->right->left = new Node(14);
    root->right->right = new Node(5);

    cout << countNodes(root);

    return 0;
}
Java
// Java program to count nodes in a complete binary tree
import java.util.*;

class Node {
    int val;
    Node left;
    Node right;
    
    Node(int x) {
        val = x;
        left = null;
        right = null;
    }
}

class GfG {
    
    // Height obtained by moving only to the left
    static int leftHeight(Node root) {
        int h = 0;
        while (root != null) {
            h++;
            root = root.left;
        }
        return h;
    }
    
    // Height obtained by moving only to the right
    static int rightHeight(Node root) {
        int h = 0;
        while (root != null) {
            h++;
            root = root.right;
        }
        return h;
    }
    
    // Count nodes using complete binary tree property
    static int countNodes(Node root) {
        if (root == null)
            return 0;
        
        int lh = leftHeight(root);
        int rh = rightHeight(root);
        
        // Perfect binary tree
        if (lh == rh)
            return (1 << lh) - 1;
        
        return 1 + countNodes(root.left) + countNodes(root.right);
    }
    
    public static void main(String[] args) {
        Node root = new Node(1);
        
        root.left = new Node(2);
        root.right = new Node(3);
        
        root.left.left = new Node(11);
        root.left.right = new Node(12);
        
        root.right.left = new Node(14);
        root.right.right = new Node(5);
        
        System.out.println(countNodes(root));
    }
}   node Node = new node();
    Node.data = data;
    Node.left = null;
    Node.right = null;
    return (Node);
}

// Driver Code
public static void main(String[] args)
{
    node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(11);
    root.left.right = newNode(12);
    root.right.left = newNode(14);
    root.right.right = newNode(5);

    System.out.print(TotalNodes(root));

}
}
Python
# Python program to count nodes in a complete binary tree

class Node:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# Height obtained by moving only to the left
def leftHeight(root):
    h = 0
    while root:
        h += 1
        root = root.left
    return h

# Height obtained by moving only to the right
def rightHeight(root):
    h = 0
    while root:
        h += 1
        root = root.right
    return h

# Count nodes using complete binary tree property
def countNodes(root):
    if root is None:
        return 0
    
    lh = leftHeight(root)
    rh = rightHeight(root)
    
    # Perfect binary tree
    if lh == rh:
        return (1 << lh) - 1
    
    return 1 + countNodes(root.left) + countNodes(root.right)

# Driver code
if __name__ == "__main__":
    root = Node(1)
    
    root.left = Node(2)
    root.right = Node(3)
    
    root.left.left = Node(11)
    root.left.right = Node(12)
    
    root.right.left = Node(14)
    root.right.right = Node(5)
    
    print(countNodes(root))
C#
// C# program to count nodes in a complete binary tree
using System;

class Node {
    public int val;
    public Node left;
    public Node right;
    
    public Node(int x) {
        val = x;
        left = null;
        right = null;
    }
}

class GfG {
    
    // Height obtained by moving only to the left
    static int leftHeight(Node root) {
        int h = 0;
        while (root != null) {
            h++;
            root = root.left;
        }
        return h;
    }
    
    // Height obtained by moving only to the right
    static int rightHeight(Node root) {
        int h = 0;
        while (root != null) {
            h++;
            root = root.right;
        }
        return h;
    }
    
    // Count nodes using complete binary tree property
    static int countNodes(Node root) {
        if (root == null)
            return 0;
        
        int lh = leftHeight(root);
        int rh = rightHeight(root);
        
        // Perfect binary tree
        if (lh == rh)
            return (1 << lh) - 1;
        
        return 1 + countNodes(root.left) + countNodes(root.right);
    }
    
    static void Main(string[] args) {
        Node root = new Node(1);
        
        root.left = new Node(2);
        root.right = new Node(3);
        
        root.left.left = new Node(11);
        root.left.right = new Node(12);
        
        root.right.left = new Node(14);
        root.right.right = new Node(5);
        
        Console.WriteLine(countNodes(root));
    }
}
JavaScript
// JavaScript program to count nodes in a complete binary tree

class Node {
    constructor(x) {
        this.val = x;
        this.left = null;
        this.right = null;
    }
}

// Height obtained by moving only to the left
function leftHeight(root) {
    let h = 0;
    while (root) {
        h++;
        root = root.left;
    }
    return h;
}

// Height obtained by moving only to the right
function rightHeight(root) {
    let h = 0;
    while (root) {
        h++;
        root = root.right;
    }
    return h;
}

// Count nodes using complete binary tree property
function countNodes(root) {
    if (root === null)
        return 0;
    
    const lh = leftHeight(root);
    const rh = rightHeight(root);
    
    // Perfect binary tree
    if (lh === rh)
        return (1 << lh) - 1;
    
    return 1 + countNodes(root.left) + countNodes(root.right);
}

// Driver code
const root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.left.left = new Node(11);
root.left.right = new Node(12);

root.right.left = new Node(14);
root.right.right = new Node(5);

console.log(countNodes(root));

Output
7
Comment