0
0
DSA Typescriptprogramming~20 mins

Count Total Nodes in Binary Tree in DSA Typescript - 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
Output of Node Count in Simple Binary Tree
What is the output of the following TypeScript code that counts total nodes in a binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function countNodes(root: TreeNode | null): number {
  if (root === null) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(countNodes(tree));
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Count the root node plus nodes in left and right subtrees.
Predict Output
intermediate
2:00remaining
Count Nodes in Unbalanced Binary Tree
What will be printed after running this code that counts nodes in an unbalanced binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function countNodes(root: TreeNode | null): number {
  if (root === null) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const tree = new TreeNode(10, new TreeNode(5, new TreeNode(3)), null);
console.log(countNodes(tree));
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Count all nodes including left subtree nodes.
🔧 Debug
advanced
2:00remaining
Identify the Error in Node Counting Function
What error will this TypeScript code produce when counting nodes in a binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function countNodes(root: TreeNode | null): number {
  if (root === null) return 0;
  return 1 + countNodes(root.left) - countNodes(root.right);
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(countNodes(tree));
ASyntaxError
B2
C1
D0
Attempts:
2 left
💡 Hint
Check the operator used between recursive calls.
Predict Output
advanced
2:00remaining
Count Nodes in Large Balanced Binary Tree
What is the output of this code counting nodes in a balanced binary tree of height 3?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function countNodes(root: TreeNode | null): number {
  if (root === null) return 0;
  return 1 + countNodes(root.left) + countNodes(root.right);
}

const tree = new TreeNode(1,
  new TreeNode(2, new TreeNode(4), new TreeNode(5)),
  new TreeNode(3, new TreeNode(6), new TreeNode(7))
);
console.log(countNodes(tree));
A7
B6
C8
D5
Attempts:
2 left
💡 Hint
Count all nodes in a full binary tree with 3 levels.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Counting Nodes in Binary Tree
What is the time complexity of the recursive function that counts total nodes in a binary tree with n nodes?
AO(n)
BO(log n)
CO(n log n)
DO(1)
Attempts:
2 left
💡 Hint
Consider how many nodes the function visits.