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 email@example.com. 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.
- Immediate Smaller element in an 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
- 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
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert an arbitrary Binary Tree to a tree that holds Children Sum 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
- Convert a given Binary tree to a tree that holds Logical AND property
- Sub-tree with minimum color difference in a 2-coloured tree
- Create a mirror tree from the given binary tree