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:

Print All Nodes Between Two Given Levels

Print All Nodes Between Two Given Levels


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

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: