0
0
DSA Javascriptprogramming~20 mins

Count Total Nodes in Binary Tree in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Count nodes in a simple binary tree
What is the output of the following code that counts total nodes in a binary tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function countNodes(root) {
  if (!root) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);

console.log(countNodes(root));
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Count each node including root, left child, right child, and left-left child.
Predict Output
intermediate
1:00remaining
Count nodes in an empty tree
What is the output when counting nodes in an empty binary tree?
DSA Javascript
function countNodes(root) {
  if (!root) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const root = null;
console.log(countNodes(root));
A0
Bnull
C1
Dundefined
Attempts:
2 left
💡 Hint
An empty tree has no nodes.
Predict Output
advanced
2:00remaining
Count nodes in a full binary tree
What is the output of counting nodes in this full binary tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function countNodes(root) {
  if (!root) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);

console.log(countNodes(root));
A8
B5
C6
D7
Attempts:
2 left
💡 Hint
Count all nodes including root, its two children, and their children.
Predict Output
advanced
2:00remaining
Count nodes in an unbalanced binary tree
What is the output of counting nodes in this unbalanced binary tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function countNodes(root) {
  if (!root) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const root = new Node(10);
root.left = new Node(5);
root.left.left = new Node(3);
root.left.left.left = new Node(1);
root.right = new Node(15);

console.log(countNodes(root));
A4
B5
C6
D3
Attempts:
2 left
💡 Hint
Count all nodes including the deep left branch.
🧠 Conceptual
expert
1:30remaining
Time complexity of counting nodes in binary tree
What is the time complexity of counting total nodes in a binary tree using a recursive function that visits each node once?
AO(n^2), where n is the number of nodes
BO(log n), where n is the number of nodes
CO(n), where n is the number of nodes
DO(1), constant time
Attempts:
2 left
💡 Hint
The function visits every node exactly once.