Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Count Complete Tree Nodes problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes