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 give 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.
2 3 5 6 4 77 15 45 80
This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Difference between Binary Heap, Binomial Heap and Fibonacci Heap
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- Difference between Min Heap and Max Heap
- Print all the leaf nodes of Binary Heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert BST to Min Heap
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Maximum element in min heap
- Fibonacci Heap - Deletion, Extract min and Decrease key
- K’th Least Element in a Min-Heap
- Maximise the number of toys that can be purchased with amount K using min Heap
- Merge two sorted arrays in constant space using Min Heap
- Min Heap in Python
- Complexity analysis of various operations of Binary Min Heap
- k largest(or smallest) elements in an array | added Min Heap method
- Height of a complete binary tree (or Heap) with N nodes
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Applications of Heap Data Structure
- Tournament Tree (Winner Tree) and Binary Heap