Given a Binary Tree, the task is for each level is to print the total number of nodes from all lower levels which are less than or equal to every node present at that level.
Input: Below is the given tree:
/ \ / \
10 2 3 1
Output: 4 3 0
Nodes in level 1 has 4 nodes as (3) in level 2 and (2, 3, 1) in level 3.
Nodes in level 2 has 3 nodes as (2, 3, 1) in level 3.
Nodes in level 3 does not have any level left below it.
Input: Below is the given tree:
/ / \
1 3 1
Output: 3 3 0
Approach: Follow the steps below to solve the problem:
- Calculate the minimum value at every level using Level Order Traversal.
- Perform Post Order Traversal on the tree and check for every node whether nodes computed in step 1 are greater than or equal to the node. If found to be true, increment count by one at that level, providing that particular level has the node present in the level below it whose value is less than or equal to all the nodes present at that level.
- Print the final array which gives the number of nodes for that level.
Below is the implementation of the above approach:
4 3 0
Time Complexity: O(N2)
Auxiliary Space: O(1)
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.
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print all nodes except rightmost node of every level of the Binary Tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Difference between sum of even and odd valued nodes in a Binary Tree
- Minimum valued node having maximum depth in an N-ary Tree
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Print alternate nodes from all levels of a Binary Tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Level order traversal with direction change after every two levels | Recursive Approach
- Level order traversal with direction change after every two levels
- Swap Nodes in Binary tree of every k'th level
- Difference between sums of odd level and even level nodes of a 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 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.