Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Count Complete Tree Nodes problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Count Complete Tree Nodes problem solution, we have given the root of a complete binary tree, return the number of the nodes in the tree.

Leetcode Count Complete Tree Nodes problem solution

Leetcode Count Complete Tree Nodes problem solution in Python.

class Solution:
    def countNodes(self, root: TreeNode) -> int:
        if not root:
            return 0
        frontier = [root]
        level = 0
        last = None
        while frontier:
            new = []
            level += 1
            for node in frontier:
                if node.left == None or node.right == None:
                    # last is the last level with full nodes
                    last = level
                new.append(node.left)
                new.append(node.right)
            
            if last == None:
                frontier = new
            else:
                temp = 0
                for i in range(last):
                    temp += 2**i
                return len([elt for elt in new if elt != None]) + temp

Count Complete Tree Nodes problem solution in Java.

int count = 0;
    public int countNodes(TreeNode root) {
        helper(root);
        return count;
    }

    private void helper(TreeNode node){
        if (node == null) return;
        count++;
        helper(node.left);
        helper(node.right);
    }

Problem solution in C++.

class Solution {
public:
    int countNodes(TreeNode* root) {
        int res = 0;
        int depth = 0;
        int tmp = 0;
        int cnt = 0;
        bool flag = true;
        bool virgin = true;
        countNodesDFS(root, depth, tmp, cnt, flag, virgin);
        int plus = 1;
        for(int i = 0; i < depth; i++){
            res += plus;
            plus *= 2;
        }
        res = res - (plus / 2) + (cnt / 2);
        return res;
    }
    
    void countNodesDFS(TreeNode* cur, int& depth, int &tmp, int &cnt, bool &flag, bool &virgin){
        if(flag == false)
            return;
        if(cur == NULL){
            if(virgin == true){
                depth = tmp;
                virgin = false;
            }
            if(tmp == depth)
                cnt++;
            else
                flag = false;
            return;
        }
        tmp += 1;
        countNodesDFS(cur->left, depth, tmp, cnt, flag, virgin);
        countNodesDFS(cur->right, depth, tmp, cnt, flag, virgin);
        tmp -= 1;
    }
    
};

Problem solution in C.

void count_nodes(struct TreeNode* root, int* size){
    if(root==NULL){
        *size=0;
        return;
    }
    int l_subtree_size, r_subtree_size;  
    count_nodes(root->left, &l_subtree_size);
    count_nodes(root->right, &r_subtree_size);
    *size=l_subtree_size+r_subtree_size+1;
}

int countNodes(struct TreeNode* root){
    int size=0;
    count_nodes(root, &size);
    return size;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes