Given an array of N elements which denotes the array representation of binary heap, the task is to find the leaf nodes of this binary heap.
Examples:
Input:
arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 4 5 6 7
Explanation:
1
/ \
2 3
/ \ / \
4 5 6 7
Leaf nodes of the Binary Heap are:
4 5 6 7
Input:
arr[] = {1, 2, 3, 4, 5,
6, 7, 8, 9, 10}
Output: 6 7 8 9 10
Explanation:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ /
8 9 10
Leaf Nodes of the Binary Heap are:
6 7 8 9 10
Approach: The key observation in the problem is that the every leaf node of the Binary Heap will be at the Height H or H -1, If H is the height of the Binary Heap. Therefore, the leaf nodes can be computed as follows:
- Calculate the total height of the binary heap.
- Traverse the array in reverse order and compare the height of each node to the compute height H of the Binary Heap.
- If the height of the current node is H, then add the current node to the leaf nodes.
- Otherwise, If the height of current node is H-1 and there are no child nodes, then also add the node as leaf node.
Below is the implementation of the above approach:
Java
// Java implementation to print // the leaf nodes of a Binary Heap import java.lang.*; import java.util.*; class GFG { // Function to calculate height // of the Binary heap with given // the count of the nodes static int height(int N) { return (int)Math.ceil( Math.log(N + 1) / Math.log(2)) - 1; } // Function to find the leaf // nodes of binary heap static void findLeafNodes( int arr[], int n) { // Calculate the height of // the complete binary tree int h = height(n); ArrayList<Integer> arrlist = new ArrayList<>(); for (int i = n - 1; i >= 0; i--) { if (height(i + 1) == h) { arrlist.add(arr[i]); } else if (height(i + 1) == h - 1 && n <= ((2 * i) + 1)) { // if the height if h-1, // then there should not // be any child nodes arrlist.add(arr[i]); } else { break; } } printLeafNodes(arrlist); } // Funtion to print the leaf nodes static void printLeafNodes( ArrayList<Integer> arrlist) { for (int i = arrlist.size() - 1; i >= 0; i--) { System.out.print( arrlist.get(i) + " "); } } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; findLeafNodes(arr, arr.length); } } |
C#
// C# implementation to print // the leaf nodes of a Binary Heap using System; using System.Collections.Generic; class GFG{ // Function to calculate height // of the Binary heap with given // the count of the nodes static int height(int N) { return (int)Math.Ceiling( Math.Log(N + 1) / Math.Log(2)) - 1; } // Function to find the leaf // nodes of binary heap static void findLeafNodes(int []arr, int n) { // Calculate the height of // the complete binary tree int h = height(n); List<int> arrlist = new List<int>(); for (int i = n - 1; i >= 0; i--) { if (height(i + 1) == h) { arrlist.Add(arr[i]); } else if (height(i + 1) == h - 1 && n <= ((2 * i) + 1)) { // if the height if h-1, // then there should not // be any child nodes arrlist.Add(arr[i]); } else { break; } } printLeafNodes(arrlist); } // Funtion to print the leaf nodes static void printLeafNodes(List<int> arrlist) { for (int i = arrlist.Count - 1; i >= 0; i--) { Console.Write(arrlist[i] + " "); } } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; findLeafNodes(arr, arr.Length); } } // This code is contributed by Princi Singh |
6 7 8 9 10
Performance Analysis:
- Time Complexity: O(L), where L is the number of leaf nodes.
- Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Leaf starting point in a Binary Heap data structure
- Print all nodes less than a value x in a Min Heap.
- Height of a complete binary tree (or Heap) with N nodes
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Maximum number of leaf nodes that can be visited within the given budget
- Binary Heap
- Array Representation Of Binary Heap
- Check if a given Binary Tree is Heap
- Why is Binary Heap Preferred over BST for Priority Queue?
- How to check if a given array represents a Binary Heap?
- Complexity analysis of various operations of Binary Min Heap
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Maximize count of set bits in a root to leaf path in a binary tree
- Heap Sort for decreasing order using min heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Tournament Tree (Winner Tree) and Binary Heap
- Print the nodes with a prime degree in given Prufer sequence of a Tree
- Distance between two nodes of binary tree with node values from 1 to N
- Check if all nodes of the Binary Tree can be represented as sum of two primes
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.
Improved By : princi singh

