Given an element x, task is to find the value of its immediate smaller element.
Input : x = 30 (for above tree) Output : Immediate smaller element is 25
Explanation : Elements 2, 15, 20 and 25 are smaller than x i.e, 30, but 25 is the immediate smaller element and hence the answer.
- Let res be the resultant node.
- Initialize the resultant Node as NULL.
- For every Node, check if data of root is greater than res, but less than x. if yes, update res.
- Recursively do the same for all nodes of the given Generic Tree.
- Return res, and res->key would be the immediate smaller element.
Below is the implementation of above approach :
Immediate smaller element of 30 is 25
Time Complexity : O(N), where N is the number of nodes in N-ary Tree.
Auxiliary Space : O(N), for recursive call(worst case when a node has N number of childs)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Next Smaller Element
- BST to a Tree with sum of all smaller keys
- Find the element before which all the elements are smaller than it, and after which all are greater
- First strictly smaller element in a sorted array in Java
- Maximize number of groups formed with size not smaller than its largest element
- Next Larger element in n-ary tree
- Second Largest element in n-ary tree
- Find the closest element in Binary Search Tree
- Sum and Product of maximum and minimum element in Binary Tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Round to next smaller multiple of 8
- Count of lexicographically smaller characters on right
- Count numbers (smaller than or equal to N) with given digit sum
- Find the farthest smaller number in the right side
- Count elements smaller than or equal to x in a sorted matrix
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Search equal, bigger or smaller in a sorted array in Java
- Find the nearest smaller numbers on left side in an array
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.