Finding the Height of a Binary Tree in Java

A binary tree is a tree data structure where each node has at most two children, typically referred to as the left child and the right child. The height (or depth) of a binary tree is the length of the longest path from the root node down to the farthest leaf node. A tree with just a root node has a height of 1.

In this blog post, we will walk you through the process of writing a Java program to find the height of a binary tree.

1. Defining the Binary Tree Node:

The foundation of a binary tree is its nodes. Here's a simple class definition for a binary tree node:
class Node {
    int data;
    Node left, right;

    public Node(int item) {
        data = item;
        left = right = null;

2. Implementing the Binary Tree Class: 

The binary tree class contains our methods for adding nodes and calculating the height.
class BinaryTree {
    Node root;

    // Compute the height of the binary tree
    int height(Node node) {
        if (node == null) {
            return 0;
        } else {
            // Compute the height of each subtree
            int leftHeight = height(node.left);
            int rightHeight = height(node.right);

            // Return the larger one
            return Math.max(leftHeight, rightHeight) + 1;

    int getHeight() {
        return height(root);

3. Testing the Program: 

Now that our binary tree and its methods are set up, let's populate it with some nodes and compute its height.

public class Main {
    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("Height of the binary tree is: " + tree.getHeight());
Height of the binary tree is: 3

Step-by-Step Explanation: 

Node Definition: We begin by defining a simple Node class that has integer data and two child nodes (left and right). 

Calculating Height: The height() method is a recursive function. If the node is null, it returns 0. Otherwise, it computes the height of the left and right subtrees and returns the greater of the two, incremented by 1. 

Building the Tree: In our test, we constructed a binary tree of height 3, where the root node has data 1, its left child has data 2, and its right child has data 3. Furthermore, the left child of the root node (with data 2) has two children: 4 (left) and 5 (right). 

Output: The output shows that our program correctly computed the height of the tree as 3.


Computing the height of a binary tree is a foundational concept in data structures. In this post, we broke down the process of defining a binary tree in Java and computing its height. Such an understanding is crucial when delving deeper into tree algorithms and operations, and it serves as a building block for more advanced topics like balanced trees and graph traversals.