Print All Nodes Between Two Given Levels
Objective: Given a binary Tree and two levels, Print all the nodes between those.
Input: A Binary Tree and two levels.
Output: Print all the nodes between given levels.
Example:
Approach:
- The solution of this problem is very simple, Do level order traversal and in recursive calls first add the right child and then left child.
- Take a global variable say, currLevel which will store the current level while doing Level Order traversal.
- Print only nodes between given levels.
Read this solution “ Level Order Traversal, Print each level in separate line” and implement the above approach.
Complete Code:
Output:
Print all nodes between nodes 2 and 4 10 15 20 25 30 35 40 45