Given a generic tree and a integer x. Find and return the node with next larger element in the tree i.e. find a node just greater than x. Return NULL if no node is present with value greater than x.
For example, in the given tree
x = 10, just greater node value is 12
The idea is maintain a node pointer res, which will contain the final resultant node.
Traverse the tree and check if root data is greater than x. If so, then compare the root data with res data.
If root data is greater than n and less than res data update res.
Next larger element of 5 is 6
This article is contributed by Chhavi. 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.
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.
- Second Largest element in n-ary tree
- Immediate Smaller element in an 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
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Convert a given Binary tree to a tree that holds Logical AND property
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Convert a given Binary tree to a tree that holds Logical OR property
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Cartesian tree from inorder traversal | Segment Tree