Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Print All Nodes Between Two Given Levels

Objec­tive: Given a binary Tree and two lev­els, Print all the nodes between those.

Input: A Binary Tree and two levels.

Out­put: Print all the nodes between given levels.

Exam­ple:

Print All Nodes Between Two Given Levels

Print All Nodes Between Two Given Levels


Approach:

  • The solu­tion of this prob­lem is very sim­ple, Do level order tra­ver­sal and in recur­sive calls first add the right child and then left child.
  • Take a global vari­able say, cur­rLevel which will store the cur­rent level while doing Level Order traversal.
  • Print only nodes between given levels.

Read this solu­tion ” Level Order Tra­ver­sal, Print each level in sep­a­rate line” and imple­ment the above approach.

Com­plete Code:


Out­put:

Print all nodes between nodes 2 and 4
10 15
20 25 30 35
40 45

You may also like...