Bird
Raised Fist0
DSA Goprogramming~10 mins

Count Total Nodes in Binary Tree in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return 0 when the node is empty (nil).

DSA Go
func countNodes(root *TreeNode) int {
    if root == [1] {
        return 0
    }
    return 1 + countNodes(root.Left) + countNodes(root.Right)
}
Drag options to blanks, or click blank then click option'
Anull
Bnil
C0
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null' instead of 'nil' causes a compile error.
Returning 0 without checking for nil causes runtime errors.
2fill in blank
medium

Complete the code to count nodes by adding 1 for the current node plus counts of left and right subtrees.

DSA Go
func countNodes(root *TreeNode) int {
    if root == nil {
        return 0
    }
    return [1] + countNodes(root.Left) + countNodes(root.Right)
}
Drag options to blanks, or click blank then click option'
A1
B0
Croot.Val
DcountNodes(root)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 0 instead of 1 results in always zero count.
Using root.Val adds node value, not count.
3fill in blank
hard

Fix the error in the recursive call to count left subtree nodes.

DSA Go
func countNodes(root *TreeNode) int {
    if root == nil {
        return 0
    }
    return 1 + countNodes([1]) + countNodes(root.Right)
}
Drag options to blanks, or click blank then click option'
Anil
Broot.Right
Croot
Droot.Left
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.Right twice counts right subtree twice.
Using root or nil causes infinite recursion or errors.
4fill in blank
hard

Fill both blanks to complete the function that counts nodes in a binary tree.

DSA Go
func countNodes(root *TreeNode) int {
    if root == [1] {
        return [2]
    }
    return 1 + countNodes(root.Left) + countNodes(root.Right)
}
Drag options to blanks, or click blank then click option'
Anil
B0
C1
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 when root is nil inflates count.
Checking for 'empty' instead of nil causes errors.
5fill in blank
hard

Fill all three blanks to complete the recursive count function with base case and recursive calls.

DSA Go
func countNodes(root *TreeNode) int {
    if root == [1] {
        return [2]
    }
    return [3] + countNodes(root.Left) + countNodes(root.Right)
}
Drag options to blanks, or click blank then click option'
Anil
B0
C1
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Returning root instead of 0 in base case.
Adding 0 instead of 1 for current node.

Practice

(1/5)
1. What does the function to count total nodes in a binary tree do?
easy
A. It counts only the leaf nodes of the tree.
B. It counts all nodes including root, left, and right children.
C. It counts only the nodes on the left side of the tree.
D. It counts only the nodes on the right side of the tree.

Solution

  1. Step 1: Understand node counting in binary tree

    Counting total nodes means including every node: root, left child, and right child recursively.
  2. Step 2: Compare options with definition

    Only the option that says 'It counts all nodes including root, left, and right children.' includes all nodes, others limit to parts of the tree.
  3. Final Answer:

    It counts all nodes including root, left, and right children. -> Option B
  4. Quick Check:

    Total nodes = all nodes in tree [OK]
Hint: Count every node, not just leaves or sides [OK]
Common Mistakes:
  • Counting only leaf nodes
  • Ignoring root node
  • Counting only one subtree
2. Which of the following Go function signatures correctly defines a function to count nodes in a binary tree with root type *Node?
easy
A. func countNodes(root *Node) string
B. func countNodes(root Node) int
C. func countNodes(root *Node) int
D. func countNodes(root Node) bool

Solution

  1. Step 1: Identify correct parameter type

    Binary tree root is usually a pointer to Node, so *Node is correct.
  2. Step 2: Identify correct return type

    Counting nodes returns an integer, so return type int is correct.
  3. Final Answer:

    func countNodes(root *Node) int -> Option C
  4. Quick Check:

    Pointer parameter and int return type [OK]
Hint: Use pointer receiver and int return for count function [OK]
Common Mistakes:
  • Passing Node by value instead of pointer
  • Returning wrong type like string or bool
  • Missing pointer in parameter
3. What is the output of the following Go code that counts nodes in a binary tree?
package main

import "fmt"

type Node struct {
  Val   int
  Left  *Node
  Right *Node
}

func countNodes(root *Node) int {
  if root == nil {
    return 0
  }
  return 1 + countNodes(root.Left) + countNodes(root.Right)
}

func main() {
  root := &Node{Val: 1}
  root.Left = &Node{Val: 2}
  root.Right = &Node{Val: 3}
  root.Left.Left = &Node{Val: 4}
  fmt.Println(countNodes(root))
}
medium
A. 6
B. 3
C. 5
D. 4

Solution

  1. Step 1: Count nodes in the tree structure

    Nodes are: 1 (root), 2 (left), 3 (right), 4 (left-left). Total = 4 nodes.
  2. Step 2: Verify counting logic in code

    Code counts root + left subtree + right subtree recursively, so total nodes = 4.
  3. Final Answer:

    4 -> Option D
  4. Quick Check:

    Count nodes recursively = 4 [OK]
Hint: Add 1 for root plus counts of left and right subtrees [OK]
Common Mistakes:
  • Counting only root and immediate children
  • Forgetting to count left-left node
  • Misreading tree structure
4. Identify the error in this Go function to count total nodes in a binary tree:
func countNodes(root *Node) int {
  if root == nil {
    return 1
  }
  return 1 + countNodes(root.Left) + countNodes(root.Right)
}
medium
A. Returns 1 when root is nil, should return 0.
B. Missing base case for leaf nodes.
C. Returns sum without counting current node.
D. Function signature is incorrect.

Solution

  1. Step 1: Check base case for nil node

    When root is nil, it means no node, so should return 0, not 1.
  2. Step 2: Verify the counting logic

    The return statement correctly adds 1 for the current node plus recurses on left and right subtrees.
  3. Final Answer:

    Returns 1 when root is nil, should return 0. -> Option A
  4. Quick Check:

    Nil node count = 0, not 1 [OK]
Hint: Base case for nil node must return 0 [OK]
Common Mistakes:
  • Returning 1 for nil node
  • Not adding 1 for current node
  • Incorrect base case logic
5. Given a binary tree where each node has a value, how would you modify the countNodes function to count only nodes with even values?
hard
A. Add a check: if node value is even, add 1; else add 0, then recurse.
B. Count all nodes and subtract nodes with odd values after traversal.
C. Only traverse left subtree if node value is even.
D. Return 1 for every node regardless of value.

Solution

  1. Step 1: Add condition to count only even nodes

    Check if current node's value is even; if yes, count 1, else 0.
  2. Step 2: Recursively count even nodes in left and right subtrees

    Sum counts from left and right subtrees with current node's count.
  3. Final Answer:

    Add a check: if node value is even, add 1; else add 0, then recurse. -> Option A
  4. Quick Check:

    Count only even nodes by conditional addition [OK]
Hint: Add 1 only if node value is even, else 0 [OK]
Common Mistakes:
  • Counting all nodes then subtracting odd nodes
  • Skipping subtrees based on node value
  • Ignoring node values in count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard