Bird
Raised Fist0
DSA Javaprogramming~20 mins

Height of Binary Tree in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Height Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the height of this binary tree?

Consider the following binary tree structure and code to calculate its height. What will be the printed height?

DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BinaryTree {
    Node root;

    int height(Node node) {
        if (node == null) return 0;
        int leftHeight = height(node.left);
        int rightHeight = height(node.right);
        return Math.max(leftHeight, rightHeight) + 1;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
        System.out.println(tree.height(tree.root));
    }
}
A3
B2
C4
D5
Attempts:
2 left
💡 Hint

Height is the number of nodes on the longest path from root to a leaf.

❓ Predict Output
intermediate
2:00remaining
What is the height of this unbalanced binary tree?

Given the following tree and height function, what is the output?

DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BinaryTree {
    Node root;

    int height(Node node) {
        if (node == null) return 0;
        int leftHeight = height(node.left);
        int rightHeight = height(node.right);
        return Math.max(leftHeight, rightHeight) + 1;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(10);
        tree.root.right = new Node(20);
        tree.root.right.right = new Node(30);
        tree.root.right.right.right = new Node(40);
        System.out.println(tree.height(tree.root));
    }
}
A2
B3
C1
D4
Attempts:
2 left
💡 Hint

Check the longest path from root to the deepest leaf node.

🔧 Debug
advanced
2:00remaining
Why does this height function return wrong height?

Look at this code snippet. It tries to calculate the height of a binary tree but returns wrong results. What is the bug?

DSA Java
int height(Node node) {
    if (node == null) return 0;
    int leftHeight = height(node.left);
    int rightHeight = height(node.right);
    return leftHeight + rightHeight + 1;
}
AIt should return 0 when node is not null, but returns 0 when null.
BIt should subtract 1 from the sum of left and right heights.
CIt adds left and right heights instead of taking the maximum, causing incorrect height calculation.
DIt does not check if node.left or node.right is null before recursion.
Attempts:
2 left
💡 Hint

Height is the longest path, not sum of paths.

🧠 Conceptual
advanced
1:00remaining
What is the height of an empty binary tree?

Consider an empty binary tree (no nodes). What is its height according to the standard definition used in the code?

A1
B0
C-1
DUndefined
Attempts:
2 left
💡 Hint

Check the base case in the height function.

🚀 Application
expert
3:00remaining
Calculate height after inserting nodes in this order

Starting with an empty binary tree, nodes are inserted in this order: 15, 10, 20, 8, 12, 17, 25. What is the height of the tree after all insertions?

DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BinaryTree {
    Node root;

    Node insert(Node node, int data) {
        if (node == null) return new Node(data);
        if (data < node.data) node.left = insert(node.left, data);
        else node.right = insert(node.right, data);
        return node;
    }

    int height(Node node) {
        if (node == null) return 0;
        int leftHeight = height(node.left);
        int rightHeight = height(node.right);
        return Math.max(leftHeight, rightHeight) + 1;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        int[] values = {15, 10, 20, 8, 12, 17, 25};
        for (int v : values) {
            tree.root = tree.insert(tree.root, v);
        }
        System.out.println(tree.height(tree.root));
    }
}
A3
B2
C4
D5
Attempts:
2 left
💡 Hint

Draw the tree after all insertions and count the longest path.

Practice

(1/5)
1. What is the height of a binary tree with only one node (the root)?
easy
A. Undefined
B. 1
C. 2
D. 0

Solution

  1. Step 1: Understand height definition

    The height of a binary tree is the number of edges on the longest path from the root to a leaf.
  2. Step 2: Apply to single node tree

    With only one node (root), the longest path has length 0 edges.
  3. Final Answer:

    0 -> Option D
  4. Quick Check:

    Single node tree height = 0 [OK]
Hint: Height counts edges on longest root-to-leaf path [OK]
Common Mistakes:
  • Thinking height is 1 for single node
  • Confusing height with number of nodes
  • Assuming height undefined for single node
2. Which of the following Java method signatures correctly defines a recursive method to find the height of a binary tree node?
easy
A. public int height(Node root)
B. int height(Node root) void
C. public void height(Node root) returns int
D. height(Node root) int public

Solution

  1. Step 1: Check Java method syntax

    Java methods must specify access modifier, return type, method name, and parameters correctly.
  2. Step 2: Validate each option

    public int height(Node root) follows correct syntax: public int height(Node root). Others have syntax errors.
  3. Final Answer:

    public int height(Node root) -> Option A
  4. Quick Check:

    Correct Java method signature = public int height(Node root) [OK]
Hint: Java method: access + return type + name + parameters [OK]
Common Mistakes:
  • Wrong order of return type and name
  • Missing return type
  • Using void with return value
3. What will be the output of the following Java code snippet?
class Node {
  int data;
  Node left, right;
  Node(int d) { data = d; left = right = null; }
}

public class Test {
  public static int height(Node root) {
    if (root == null) return 0;
    int leftHeight = height(root.left);
    int rightHeight = height(root.right);
    return Math.max(leftHeight, rightHeight) + 1;
  }

  public static void main(String[] args) {
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    System.out.println(height(root));
  }
}
medium
A. 1
B. 3
C. 2
D. 4

Solution

  1. Step 1: Visualize the tree structure

    The tree has root 1, left child 2, right child 3, and left child of 2 is 4. The longest path is 1->2->4.
  2. Step 2: Calculate height recursively

    Height is max depth: left subtree height = 2 (nodes 2->4), right subtree height = 1 (node 3), so total height = max(2,1)+1 = 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Longest path length = 3 [OK]
Hint: Height = longest root-to-leaf path node count [OK]
Common Mistakes:
  • Counting edges instead of nodes
  • Ignoring null child base case
  • Misreading tree structure
4. Identify the error in the following Java method to find the height of a binary tree:
public int height(Node root) {
  if (root == null) {
    return -1;
  }
  int leftHeight = height(root.left);
  int rightHeight = height(root.right);
  return Math.max(leftHeight, rightHeight);
}
medium
A. The method should not use recursion.
B. The method should return the sum of left and right heights.
C. The base case should return 0, not -1.
D. The method should return the minimum of left and right heights.

Solution

  1. Step 1: Check base case return value

    Height of empty tree (null) should be 0 or consistent with counting edges. Returning -1 causes incorrect height calculation.
  2. Step 2: Check return statement

    Method returns max of left and right heights but misses adding 1 for current node, so height is underestimated.
  3. Final Answer:

    The base case should return 0, not -1. -> Option C
  4. Quick Check:

    Base case must return 0 for correct height [OK]
Hint: Base case return 0, add 1 after max height [OK]
Common Mistakes:
  • Returning -1 in base case
  • Not adding 1 to max height
  • Returning sum instead of max
5. Given a binary tree where each node stores an integer, write a method to find the height of the tree and then print all nodes at the maximum height level. Which approach correctly combines these tasks?
hard
A. First find height recursively, then use a level-order traversal to print nodes at that height.
B. Print nodes during height calculation without storing height separately.
C. Use in-order traversal to print nodes at maximum height level directly.
D. Calculate height by counting nodes and print nodes at root only.

Solution

  1. Step 1: Find height recursively

    Use a recursive method to find the height of the tree, which gives the maximum depth level.
  2. Step 2: Use level-order traversal

    Perform a breadth-first traversal (level-order) and print nodes only at the level equal to the height found.
  3. Final Answer:

    First find height recursively, then use a level-order traversal to print nodes at that height. -> Option A
  4. Quick Check:

    Separate height calculation + level-order print = First find height recursively, then use a level-order traversal to print nodes at that height. [OK]
Hint: Find height first, then print nodes at that level [OK]
Common Mistakes:
  • Trying to print during height recursion
  • Using in-order traversal for level nodes
  • Printing root only ignoring height
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