Data Structure Binary Trees Practice Questions with Solutions

Introductions

Binary Trees are a special type of tree in which each node can have at most two children: a left child and a right child. These practice questions focus on solving problems rather than memorizing definitions. You will practice creating binary tree nodes, identifying children, counting nodes and leaf nodes, finding height, searching values, calculating sums, and performing preorder, inorder, and postorder traversals. Data Structure Binary Trees Practice Questions with Solutions help to understand the concepts.

Question 1: Create a Simple Binary Tree

Question

Create the following binary tree using JavaScript:

        10
       /  \
      20   30

Print the root, left child, and right child.

Solution

A binary tree node can contain:

  • A value
  • A reference to the left child
  • A reference to the right child

Create a Node class:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

Now create the nodes:

let root = new Node(10);
let leftChild = new Node(20);
let rightChild = new Node(30);

Connect them:

root.left = leftChild;
root.right = rightChild;

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

console.log("Root:", root.value);
console.log("Left Child:", root.left.value);
console.log("Right Child:", root.right.value);

The tree looks like:

        10
       /  \
      20   30

Output

Root: 10
Left Child: 20
Right Child: 30

Answer

The root is 10, the left child is 20, and the right child is 30.


Question 2: Count the Total Number of Nodes

Question

Count the number of nodes in this binary tree:

          10
        /    \
       20     30
      / \    /
     40  50 60

Solution

The nodes are:

10
20
30
40
50
60

Therefore, there are 6 nodes.

We can calculate the count recursively.

For every node:

Total Nodes = 1 + Left Subtree Nodes + Right Subtree Nodes

Code:

function countNodes(root) {
    if (root === null) {
        return 0;
    }

    return 1 +
           countNodes(root.left) +
           countNodes(root.right);
}

Create the tree:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.left = new Node(60);

console.log(countNodes(root));

Output

6

Answer

The binary tree contains 6 nodes.


Question 3: Count the Leaf Nodes

Question

Count the leaf nodes in this binary tree:

          10
        /    \
       20     30
      / \      \
     40  50     60

Solution

A leaf node has no left child and no right child.

Here:

40 → Leaf
50 → Leaf
60 → Leaf

Therefore:

Number of leaf nodes = 3

We can solve it recursively.

function countLeaves(root) {

    if (root === null) {
        return 0;
    }

    if (root.left === null && root.right === null) {
        return 1;
    }

    return countLeaves(root.left) +
           countLeaves(root.right);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.right = new Node(60);

console.log(countLeaves(root));

Output

3

Answer

The binary tree has 3 leaf nodes: 40, 50, and 60.


Question 4: Find the Height of a Binary Tree

Question

Find the height of this binary tree:

          10
        /    \
       20     30
      / \
     40  50

Consider the height as the number of edges in the longest path from the root to a leaf.

Solution

The longest paths are:

10 → 20 → 40

and

10 → 20 → 50

Each path contains 2 edges.

Therefore:

Height = 2

Recursive formula:

Height = 1 + maximum(left height, right height)

Code:

function height(root) {

    if (root === null) {
        return -1;
    }

    return 1 + Math.max(
        height(root.left),
        height(root.right)
    );
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

console.log(height(root));

Output

2

Answer

The height of the binary tree is 2 edges.


Question 5: Search for a Value in a Binary Tree

Question

Search for the value 50 in this binary tree:

          10
        /    \
       20     30
      / \    /
     40  50 60

Return true if the value exists.

Solution

A normal binary tree does not necessarily follow any ordering rule, so we may need to search both the left and right subtrees.

First, check the current node.

If it is not the target, search the left subtree.

If it is not found there, search the right subtree.

function search(root, target) {

    if (root === null) {
        return false;
    }

    if (root.value === target) {
        return true;
    }

    return search(root.left, target) ||
           search(root.right, target);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.left = new Node(60);

console.log(search(root, 50));

The search eventually reaches node 50.

Output

true

Answer

The value 50 exists in the binary tree.


Question 6: Find the Sum of All Nodes

Question

Find the sum of all nodes in this binary tree:

          10
        /    \
       20     30
      / \    /
     40  50 60

Solution

Add all values:

10 + 20 + 30 + 40 + 50 + 60

Step by step:

10 + 20 = 30
30 + 30 = 60
60 + 40 = 100
100 + 50 = 150
150 + 60 = 210

So the answer is 210.

Using recursion:

function sumNodes(root) {

    if (root === null) {
        return 0;
    }

    return root.value +
           sumNodes(root.left) +
           sumNodes(root.right);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.left = new Node(60);

console.log(sumNodes(root));

Output

210

Answer

The sum of all nodes is 210.


Question 7: Perform Preorder Traversal

Question

Perform preorder traversal of this binary tree:

          10
        /    \
       20     30
      / \    /
     40  50 60

Solution

Preorder traversal follows:

Root → Left → Right

Start at 10:

10

Then visit the left subtree:

20 → 40 → 50

Finally visit the right subtree:

30 → 60

Therefore:

10 → 20 → 40 → 50 → 30 → 60

Recursive code:

function preorder(root) {

    if (root === null) {
        return;
    }

    console.log(root.value);

    preorder(root.left);
    preorder(root.right);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.left = new Node(60);

preorder(root);

Output

10
20
40
50
30
60

Answer

The preorder traversal is:

10 → 20 → 40 → 50 → 30 → 60

Question 8: Perform Inorder Traversal

Question

Perform inorder traversal of this binary tree:

          10
        /    \
       20     30
      / \    /
     40  50 60

Solution

Inorder traversal follows:

Left → Root → Right

Start with the left subtree of 10.

For node 20:

40 → 20 → 50

Then visit the root:

10

Finally process the right subtree:

60 → 30

Therefore:

40 → 20 → 50 → 10 → 60 → 30

Recursive code:

function inorder(root) {

    if (root === null) {
        return;
    }

    inorder(root.left);

    console.log(root.value);

    inorder(root.right);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.left = new Node(60);

inorder(root);

Output

40
20
50
10
60
30

Answer

The inorder traversal is:

40 → 20 → 50 → 10 → 60 → 30

Question 9: Perform Postorder Traversal

Question

Perform postorder traversal of this binary tree:

          10
        /    \
       20     30
      / \    /
     40  50 60

Solution

Postorder traversal follows:

Left → Right → Root

For the left subtree:

40 → 50 → 20

For the right subtree:

60 → 30

Finally, visit the root:

10

Therefore:

40 → 50 → 20 → 60 → 30 → 10

Recursive code:

function postorder(root) {

    if (root === null) {
        return;
    }

    postorder(root.left);
    postorder(root.right);

    console.log(root.value);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.left = new Node(60);

postorder(root);

Output

40
50
20
60
30
10

Answer

The postorder traversal is:

40 → 50 → 20 → 60 → 30 → 10

Question 10: Count Nodes with Two Children

Question

Find how many nodes in this binary tree have both a left child and a right child:

          10
        /    \
       20     30
      / \      \
     40  50     60

Solution

Check each node.

Node 10:

Left = 20
Right = 30

Both children exist.

Node 20:

Left = 40
Right = 50

Both children exist.

Node 30:

Left = null
Right = 60

Only one child exists.

Nodes 40, 50, and 60 have no children.

Therefore, the answer is:

2

We can solve it recursively:

function countFullNodes(root) {

    if (root === null) {
        return 0;
    }

    let count = 0;

    if (root.left !== null && root.right !== null) {
        count = 1;
    }

    return count +
           countFullNodes(root.left) +
           countFullNodes(root.right);
}

Complete code:

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

let root = new Node(10);

root.left = new Node(20);
root.right = new Node(30);

root.left.left = new Node(40);
root.left.right = new Node(50);

root.right.right = new Node(60);

console.log(countFullNodes(root));

Output

2

Answer

There are 2 nodes with exactly two children: 10 and 20.

Key Takeaways

  • A binary tree is a tree where each node can have at most two children.
  • The two possible children are called the left child and right child.
  • A binary tree node can be represented using a value, left reference, and right reference.
  • Recursive functions are commonly used to solve binary tree problems.
  • Preorder traversal follows Root → Left → Right.
  • Inorder traversal follows Left → Root → Right.
  • Postorder traversal follows Left → Right → Root.
  • A leaf node has no left or right child.
  • Binary tree height can be calculated recursively.
  • Searching a normal binary tree may require checking both subtrees.
  • Counting nodes and calculating the sum of nodes are common binary tree problems.
  • A node with both left and right children is different from a node with only one child.
  • Binary Trees are the foundation for more specialized structures such as Binary Search Trees, AVL Trees, and Heaps.

FAQs

1. What is a binary tree in data structures?

A binary tree is a non-linear data structure where each node can have at most two children: a left child and a right child.

2. Can a binary tree node have only one child?

Yes. A node in a binary tree can have zero, one, or two children.

3. What is a leaf node in a binary tree?

A leaf node is a node that has neither a left child nor a right child.

4. What are the main binary tree traversal methods?

The main depth-first traversal methods are preorder, inorder, and postorder. Level-order traversal is another common method that processes nodes level by level.

5. What is preorder traversal?

Preorder traversal visits the nodes in this order: Root → Left → Right.

6. What is inorder traversal?

Inorder traversal visits the nodes in this order: Left → Root → Right.

7. What is postorder traversal?

Postorder traversal visits the nodes in this order: Left → Right → Root.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top