VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-all-nodes-less-than-a-value-x-in-a-min-heap/

⇱ Print all nodes less than a value x in a Min Heap. - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all nodes less than a value x in a Min Heap.

Last Updated : 24 Jan, 2023

Given a binary min heap and a value x, print all the binary heap nodes having value less than the given value x.

Examples : Consider the below min heap as
 common input two both below examples.
 2
 / \
 3 15
 / \ / \
 5 4 45 80
 / \ / \
 6 150 77 120

Input : x = 15 
Output : 2 3 5 6 4

Input : x = 80
Output : 2 3 5 6 4 77 15 45

The idea is to do a preorder traversal of the given Binary heap. While doing preorder traversal, if the value of a node is greater than the given value x, we return to the previous recursive call. Because all children nodes in a min heap are greater than the parent node. Otherwise we print current node and recur for its children. 

Implementation:


Output
2 3 5 6 4 77 15 45 80 

Time Complexity: O(n)
Auxiliary Space: O(1)
 

Comment
Article Tags:
Article Tags: