VOOZH about

URL: https://www.geeksforgeeks.org/dsa/height-complete-binary-tree-heap-n-nodes/

โ‡ฑ Height of a complete binary tree (or Heap) with N nodes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Height of a complete binary tree (or Heap) with N nodes

Last Updated : 4 Feb, 2026

Consider a Binary Heap of size N. Determine the height of the heap.

Examples:

Input : N = 6

๐Ÿ‘ tree

Output : 2
Explanation: The height of a tree is the number of edges on the longest path from root to leaf. Here, the longest path (e.g., 1 - 3 - 5) has 2 edges, so the treeโ€™s height is 2.

Input : N = 9

๐Ÿ‘ ttree

Output : 3
Explanation: In this tree the longest path (e.g., 2 - 3 - 5 - 6) has 3 edges, so the treeโ€™s height is 3.

[Approach] Using Mathematical Formula - O(1) Time and O(1) Space

Since a binary heap is a complete binary tree, its height is found using the formula floor(log2N).


Output
2
Comment
Article Tags:
Article Tags: