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.
- 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.
Print all nodes between nodes 2 and 4 10 15 20 25 30 35 40 45
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.