The Wayback Machine - https://web.archive.org/web/20241118060938/https://www.geeksforgeeks.org/relationship-number-nodes-height-binary-tree/
Open In App

Relationship between number of nodes and height of binary tree

Last Updated : 05 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 a maximum of two children. 

Calculating minimum and maximum height from a number of nodes – 
If there are n nodes in a binary tree, the maximum height of the binary tree is n-1, and the minimum height is floor (log2(n)+1). 
For example, the left skewed binary tree shown in Figure 1(a) with 5 nodes has a height of 5-1 = 4, and the binary tree shown in Figure 1(b) with 5 nodes has a height floor(log25) = 2. 

11

Calculating minimum and maximum number of nodes from height:
If binary tree has height h, minimum number of nodes is h+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.

222

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 ceil(log2(n)+1). 

Calculating minimum and maximum number of nodes from height:
If binary search tree has height h, minimum number of nodes is h+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. 


Previous Article
Next Article

Similar Reads

Print the nodes of the Binary Tree whose height is a Prime number
Given a binary tree, our task is to print the nodes whose height is a prime number starting from the root node. Examples: Input: 1 / \ 2 3 / \ 4 5 Output: 4 5 Explanation: For this tree: Height of Node 1 - 0, Height of Node 2 - 1, Height of Node 3 - 1, Height of Node 4 - 2, Height of Node 5 - 2. Hence, the nodes whose height is a prime number are 4
8 min read
Height of a complete binary tree (or Heap) with N nodes
Consider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we take a few examples, we can notice that the value of
3 min read
Find height of a special binary tree whose leaf nodes are connected
Given a special binary tree whose leaf nodes are connected to form a circular doubly linked list, find its height. For example, 1 / \ 2 3 / \ 4 5 / 6 In the above binary tree, 6, 5 and 3 are leaf nodes and they form a circular doubly linked list. Here, the left pointer of leaf node will act as a previous pointer of circular doubly linked list and i
9 min read
Traversal of tree with k jumps allowed between nodes of same height
There is a tree with N nodes and node 1 is the root node. Each node of the tree can contain fruit or not. Initially, you are at the root node and start climbing on the tree. You can jump from a node to any node at the same level(i.e. the height of nodes from the root are same), During climbing from root node you can only make maximum K jumps. (K
12 min read
Minimum number of nodes in an AVL Tree with given height
Given the height of an AVL tree 'h', the task is to find the minimum number of nodes the tree can have. Examples : Input : H = 0 Output : N = 1 Only '1' node is possible if the height of the tree is '0' which is the root node. Input : H = 3 Output : N = 7 Recursive Approach : In an AVL tree, we have to maintain the height balance property, i.e. dif
7 min read
Number of leaf nodes in a perfect N-ary tree of height K
Find the number of leaf nodes in a perfect N-ary tree of height K. Note: As the answer can be very large, return the answer modulo 109+7. Examples: Input: N = 2, K = 2Output: 4Explanation: A perfect Binary tree of height 2 has 4 leaf nodes. Input: N = 2, K = 1Output: 2Explanation: A perfect Binary tree of height 1 has 2 leaf nodes. Approach: This p
4 min read
Number of Binary Search Trees of height H consisting of H+1 nodes
Given a positive integer H, the task is to find the number of possible Binary Search Trees of height H consisting of the first (H + 1) natural numbers as the node values. Since the count can be very large, print it to modulo 109 + 7. Examples: Input: H = 2Output: 4Explanation: All possible BSTs of height 2 consisting of 3 nodes are as follows: Ther
6 min read
Check if a given Binary Tree is height balanced like a Red-Black Tree
In a Red-Black Tree, the maximum height of a node is at most twice the minimum height (The four Red-Black tree properties make sure this is always followed). Given a Binary Search Tree, we need to check for following property. For every node, length of the longest leaf to node path has not more than twice the nodes on shortest path from node to lea
9 min read
Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
Given a tree, and the weights (in the form of strings) of all the nodes, the task is to count the nodes whose weighted string when concatenated with the strings of the sub-tree nodes becomes a pangram. Pangram: A pangram is a sentence containing every letter of the English Alphabet. Examples: Input: Output: 1 Only the weighted string of sub-tree of
7 min read
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Comparison between Height Balanced Tree and Weight Balanced Tree
What is Height Balanced Tree? Self-Balancing binary search trees are the height-balanced binary tree is one for which at every node, the absolute value of the difference in heights of the left and right subtree is no larger than one. An empty tree is height-balanced. A non-empty binary tree T is balanced if: The left subtree of T is balanced.The ri
4 min read
Sum of nodes in a binary tree having only the left child nodes
Given a binary tree, the task is to find the sum of binary tree nodes having only the left child nodes. Example: Input: 8 / \ 3 7 / \ / 5 6 0 / / 1 2Output: 18 Explanation: Nodes with values 5, 6, and 7 are the ones that have only the left child nodes Input: 2 / \ 3 1 / / 5 6Output: 4 Approach: The given problem can be solved by traversing the bina
6 min read
Count of nodes in a binary tree having their nodes in range [L, R]
Given a Binary Tree consisting of n nodes and two positive integers l and r, the task is to find the count of nodes having their value in the range [l, r]. Examples: Input: l = 4, r = 15 Output: 4Explanation: The nodes in the given Tree that lies in the range [4, 15] are {4, 5, 6, 7}. Input: l = 1, r = 8 Output: 2Explanation: The nodes in the given
10 min read
Convert given Binary Tree to Symmetric Tree by adding minimum number of nodes
Given a Binary Tree, the task is to convert the given Binary Tree to the Symmetric Tree by adding the minimum number of nodes in the given Tree. Examples: Input: Output: Input: Output: Approach: To solve this problem, follow the below steps: Make a function buildSymmetricTree which will accept two parameters root1 and root2.Here, root1 and root2, a
8 min read
Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
Given a binary tree consisting of distinct values and two numbers K1 and K2, the task is to find all nodes that lie between them in the inorder sequence of the tree. Examples: Input: 1 / \ 12 11 / / \ 3 4 13 \ / 15 9 k1 = 12 k2 = 15 Output: 1 4 Explanation: Inorder sequence is 3 12 1 4 15 11 9 13 The common nodes between 12 and 15 in the inorder se
10 min read
Count of all prime weight nodes between given nodes in the given Tree
Given a weighted tree containing N nodes, and two nodes u and v, the task is to find the count of nodes having prime weight on the simple path between u and v (both inclusive). Examples: Input: u = 3, v = 5 Output: 2 Explanation: Prime weight on path 3 to 5 is [11, 5]. Hence, the answer is 2. Approach: To solve the problem mentioned above, the idea
15 min read
Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1
Given a matrix mat][][] of size M x N which represents the topographic map of a region, and 0 denotes land and 1 denotes elevation, the task is to maximize the height in the matrix by assigning each cell a non-negative height such that the height of a land cell is 0 and two adjacent cells must have an absolute height difference of at most 1. Exampl
12 min read
Find a set of at most N/2 nodes from a Graph such that all remaining nodes are directly connected to one of the chosen nodes
Given an integer N, representing the number of nodes present in an undirected graph, with each node valued from 1 to N, and a 2D array Edges[][], representing the pair of vertices connected by an edge, the task is to find a set of at most N/2 nodes such that nodes that are not present in the set, are connected adjacent to any one of the nodes prese
12 min read
Difference between Binary Tree and Binary Search Tree
Binary Tree Data Structure:A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right children. Binary Search Tree Data Structure (BST):A binary search tree is a hierarchical data structure where each node has at most two children, wi
2 min read
Minimum number of nodes to be selected to light up all the edges of the tree (Select Nodes)
Given N nodes of a tree and a list of edges. Find the minimum number of nodes to be selected to light up all the edges of the tree. An edge lights up when at least one node at the end of the edge is selected. Example: Input: N = 6, edges[] = {(1,2), (1,3), (2,4), (3,5), (3,6)}Output: 2Explanation: Selecting nodes 2 and 3 lights up all the edges. In
10 min read
Construct XOR tree by Given leaf nodes of Perfect Binary Tree
Given the leaf nodes of a perfect binary tree, the task is to construct the XOR tree and print the root node of this tree. An XOR tree is a tree whose parent node is the XOR of the left child and the right child node of the tree. Parent node = Left child node ^ Right child node Examples: Input: arr = {40, 32, 12, 1, 4, 3, 2, 7} Output: Nodes of the
12 min read
Count of nodes in given N-ary tree such that their subtree is a Binary Tree
Given an N-ary tree root, the task is to find the count of nodes such that their subtree is a binary tree. Example: Input: Tree in the image below Output: 11Explanation: The nodes such that there subtree is a binary tree are {2, 8, 10, 6, 7, 3, 1, 9, 5, 11, 12}. Input: Tree in the image below Output: 9 Approach: The given problem can be solved by u
11 min read
Query for ancestor-descendant relationship in a tree
Given a rooted tree with N vertices and N-1 edges. We will be given many pairs of vertices u and v, we need to tell whether u is an ancestor of v or not. Given tree will be rooted at the vertex with index 0. Examples: u = 1 v = 6 we can see from above tree that node 1 is ancestor of node 6 so the answer will be yes. u = 1 v = 7 we can see from abov
9 min read
Calculate height of Binary Tree using Inorder and Level Order Traversal
Given inorder traversal and Level Order traversal of a Binary Tree. The task is to calculate the height of the tree without constructing it. Example: Input : Input: Two arrays that represent Inorder and level order traversals of a Binary Tree in[] = {4, 8, 10, 12, 14, 20, 22}; level[] = {20, 8, 22, 4, 12, 10, 14}; Output : 4 The binary tree that ca
12 min read
Height and Depth of a node in a Binary Tree
Given a Binary Tree consisting of N nodes and a integer K, the task is to find the depth and height of the node with value K in the Binary Tree. The depth of a node is the number of edges present in path from the root node of a tree to that node.The height of a node is the number of edges present in the longest path connecting that node to a leaf n
15+ min read
Print levels with odd number of nodes and even number of nodes
Given an N-ary tree, print all the levels with odd and even numbers of nodes in it. Examples: For example consider the following tree 1 - Level 1 / \ 2 3 - Level 2 / \ \ 4 5 6 - Level 3 / \ / 7 8 9 - Level 4The levels with odd number of nodes are: 1 3 4 The levels with even number of nodes are: 2Note: The level numbers starts from 1. That is, the r
15+ min read
Iterative Method to find Height of Binary Tree
There are two conventions to define the height of a Binary Tree Number of nodes on the longest path from the root to the deepest node. Number of edges on the longest path from the root to the deepest node. In this post, the first convention is followed. For example, the height of the below tree is 3. The recursive method to find the height of the B
8 min read
Height of binary tree considering even level leaves only
Find the height of the binary tree given that only the nodes on the even levels are considered valid leaf nodes. The height of a binary tree is the number of edges between the tree's root and its furthest leaf. But what if we bring a twist and change the definition of a leaf node? Let us define a valid leaf node as the node that has no children and
8 min read
Maximum height of the binary search tree created from the given array
Given an array arr[] of N integers, the task is to make two binary search trees. One while traversing from the left side of the array and another while traversing from the right and find which tree has a greater height.Examples: Input: arr[] = {2, 1, 3, 4} Output: 0 BST starting from first index BST starting from last index 2 4 / \ / 1 3 3 \ / 4 1
10 min read
Introduction to Height Balanced Binary Tree
A height-balanced binary tree is defined as a binary tree in which the height of the left and the right subtree of any node differ by not more than 1. AVL tree, red-black tree are examples of height-balanced trees. Conditions for Height-Balanced Binary Tree: Following are the conditions for a height-balanced binary tree: The difference between the
5 min read
three90RightbarBannerImg