Given a Binary Tree with positive values at each node, the task is to print the maximum number that can be formed by arranging nodes at each level.
Input: 4 / \ 2 59 / \ / \ 1 3 2 6 Output: Maximum number at 0'th level is 4 Maximum number at 1'st level is 592 Maximum number at 2'nd level is 6321 Input: 1 / \ 2 3 / \ \ 4 5 8 / \ 6 79 Output: Explanation : The maximum number at the 0'th level is 1 The maximum number at 1'st level is 32 The maximum number at 2'nd level is 854 The maximum number at 3'rd level is 796
- Traverse all nodes at each level one by one using Level Order Traversal.
- Store their values in a vector of strings.
- Sort the vector using Comparison method to generate greatest number possible.
- Display the number and repeat the procedure for all levels.
Below code is the implementation of the above approach:
1 32 854 76
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.
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Largest value in each level of Binary Tree
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Difference between sums of odd level and even level nodes of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Find farthest node from each node in Tree
- Farthest distance of a Node from each Node of a Tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Smallest value in each level of Binary Tree
- Check if two trees are mirror of each other using level order traversal
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Count number of times each Edge appears in all possible paths of a given Tree
- Get Level of a node in a Binary Tree
- Find depth of the deepest odd level leaf node
- Depth of the deepest odd level node in Binary Tree
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.