Prerequisite – Binary Tree Data Structure

In this article, we will discuss various cases for relationship between number of nodes and height of binary tree. Before understanding this article, you should have basic idea about binary trees and their properties.

The **height** of the binary tree is the longest path from root **node to any leaf** node in the tree. For example, the height of binary tree shown in Figure 1(b) is 2 as longest path from root node to node 2 is 2. Also, the height of binary tree shown in Figure 1(a) is 4.

**Binary Tree –**

In a binary tree, a node can have maximum two children.

**Calculating minimum and maximum height from number of nodes –**

If there are n nodes in binary tree, **maximum height** of the binary tree is **n-1** and **minimum height** is **floor(log2n)**.

For example, left skewed binary tree shown in Figure 1(a) with 5 nodes has height 5-1 = 4 and binary tree shown in Figure 1(b) with 5 nodes has height floor(log25) = 2.

**Calculating minimum and maximum number of nodes from height –**

If binary tree has **height h**, minimum number of **nodes is n+1** (in case of left skewed and right skewed binary tree).

For example, the binary tree shown in Figure 2(a) with height 2 has 3 nodes.

If binary tree has height h, maximum number of nodes will be when all levels are completely full. Total number of nodes will be 2^0 + 2^1 + …. 2^h = 2^(h+1)-1.

For example, the binary tree shown in Figure 2(b) with height 2 has 2^(2+1)-1 = 7 nodes.

**Binary Search Tree –**

In a binary search tree, left child of a node has value less than the parent and right child has value greater than parent.

**Calculating minimum and maximum height from the number of nodes –**

If there are n nodes in a binary search tree, maximum height of the binary search tree is n-1 and minimum height is floor(log2n).

**Calculating minimum and maximum number of nodes from height –**

If binary search tree has **height h**, minimum number of **nodes is n+1** (in case of left skewed and right skewed binary search tree).

If binary search tree has **height h**, maximum number of nodes will be when all levels are completely full. Total number of nodes will be 2^0 + 2^1 + …. 2^h = 2^(h+1)-1.

All the rules in BST are same as in binary tree and can be visualized in the same way.

**Que-1.** The height of a tree is the length of the longest root-to-leaf path in it. The maximum and the minimum number of nodes in a binary tree of height 5 are:

(A) 63 and 6, respectively

(B) 64 and 5, respectively

(C) 32 and 6, respectively

(D) 31 and 5, respectively

**Solution:** According to formula discussed,

max number of nodes = 2^(h+1)-1 = 2^6-1 =63.

min number of nodes = h+1 = 5+1 = 6.

**Que-2.** Which of the following height is not possible for a binary tree with 50 nodes?

(A) 4

(B) 5

(C) 6

(D) None

**Solution:** According to formula discussed,

Minimum height with 50 nodes = floor(log250) = 5. Therefore, height 4 is not possible.

## Recommended Posts:

- Graph measurements: length, distance, diameter, eccentricity, radius, center
- Height of a complete binary tree (or Heap) with N nodes
- Practice questions on Height balanced/AVL Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Select a Random Node from a tree with equal probability
- Mathematics | Power Set and its Properties
- Mathematics | Graph Isomorphisms and Connectivity
- Find Minimum Depth of a Binary Tree
- Binary Tree | Set 3 (Types of Binary Tree)
- Handshaking Lemma and Interesting Tree Properties
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Binary Tree | Set 1 (Introduction)
- How to determine if a binary tree is height-balanced?
- Write a Program to Find the Maximum Depth or Height of a Tree
- Tree Traversals (Inorder, Preorder and Postorder)

This article is contributed by **Sonal Tuteja**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.