Is There an Easy Way to Measure the Height of a Tree?


#include <iostream>#include <queue>using namespace std;// This approach counts the number of nodes from root to the// leaf to calculate the height of the tree.// Defining the structure of a Node.class Node {public:int data;Node* left;Node* right;};// Helper function to create a newnode.// Input: Data for the newnode.// Return: Address of the newly created node.Node* createNode(int data){Node* newnode = new Node();newnode->data = data;newnode->left = NULL;newnode->right = NULL;return newnode;}// Function to calculate the height of given Binary Tree.// Input: Address of the root node of Binary Tree.// Return: Height of Binary Tree as a integer. This includes// the number of nodes from root to the calculateHeight(Node* root){queue<Node*> nodesInLevel;int height = 0;int nodeCount = 0; // Calculate  number of nodes in a level.Node* currentNode; // Pointer to store the address of a// node in the current level.if (root == NULL) {return 0;}nodesInLevel.push(root);while (!nodesInLevel.empty()) {// This while loop runs for every level and// increases the height by 1 in each iteration. If// the queue is empty then it implies that the last// level of tree has been parsed.height++;// Create another while loop which will insert all// the child nodes of the current level in the// queue.nodeCount = nodesInLevel.size();while (nodeCount–) {currentNode = nodesInLevel.front();// Check if the current nodes has left child and// insert it in the queue.if (currentNode->left != NULL) {nodesInLevel.push(currentNode->left);}// Check if the current nodes has right child// and insert it in the queue.if (currentNode->right != NULL) {nodesInLevel.push(currentNode->right);}// Once the children of the current node are// inserted. Delete the current node.nodesInLevel.pop();}}return height;}// Driver main(){// Creating a binary tree.Node* root = NULL;root = createNode(1);root->left = createNode(2);root->left->left = createNode(4);root->left->right = createNode(5);root->right = createNode(3);cout << “The height of the binary tree using iterative “”method is: ” << calculateHeight(root) << “.”;return 0;}



Following is the implementation in Java:


Algorithm for Calculating Height of a Binary Tree

There are two methods to approach this problem statement. First Approach is based on Depth first seach using recursion, and the other method is based on Breadth first search using Level order traversal without using recursion. 

What tools do I need for estimating tree height?

You may think that the trigonometrical method is not very practical. After all, how are you supposed to know the angles? One way could be to use a clinometer, a tool created specifically to determine elevation angle. However, if you do not own one, there is another way. Most of us carry exceptionally versatile devices in our pockets – our smartphones. There are (free!) apps designed specifically to assist you in measuring not only the angles but also the distance. Our calculator also works on mobile browsers so that you can input the results straight away!


// An iterative C# program to// find height of binary treeusing System;using System.Collections.Generic;// A binary tree nodeclass Node{public int data;public Node left, right;public Node(int item){data = item;left = right;}}public class BinaryTree{Node root;// Iterative method to find// height of Binary Treeint treeHeight(Node node){// Base Caseif (node == null)return 0;// Create an empty queue// for level order traversalQueue<Node> q = new Queue<Node>();// Enqueue Root and initialize heightq.Enqueue(node);int height = 0;while (1 == 1){// nodeCount (queue size) indicates// number of nodes at current nodeCount = q.Count;if (nodeCount == 0)return height;height++;// Dequeue all nodes of current// level and Enqueue all// nodes of next levelwhile (nodeCount > 0){Node newnode = q.Peek();queue();if (newnode.left != null)q.Enqueue(newnode.left);if (newnode.right != null)q.Enqueue(newnode.right);nodeCount–;}}}// Driver codepublic static void Main(String []args){BinaryTree tree = new BinaryTree(); // Let us create a binary// tree shown in above diagramtree.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);Console.WriteLine(“Height of tree is ” +tree.treeHeight(tree.root));}}// This code has been contributed by 29AjayKumar

Use an open container of water, such as a bucket, the easy way

When standing, measure the height of your eyes, say 63″. Fill a container with water, then measure the water depth, say 8″. Subtract the two (55″).

Walk away from the tree and find the spot where the reflection of the top of tree is in the center of the bucket when the bucket is on the ground and you are standing 55″ away from the center of the bucket. This sets up 45 degree angles.

The bucket is now as far away from the tree as the tree is tall, assuming your feet are at the same elevation as the tree base. (If not, then the bucket is now as far away from the tree as the top of the tree is above your feet)

Things You’ll Need

  • A friend (optional for most methods, but a little help makes the process easier and more fun)
  • either a tape measure or yardstick (meter ruler)
  • or a clinometer or transit
  • or a piece of paper
  • plus a pencil (for one method)


In this tutorial, we learned how to find the level order traversal of a binary tree, a basic introduction of binary trees and its applications. We also learned the meaning of height of a binary tree using two approaches, first where we apply depth first search using recursive and  in second approach we apply breadth first search without recursion using a queue data structure.