Replace Node With Depth

Given a a binary tree. Replace each of it's data with the depth of tree.

Root is at depth 0, change its value to 0 and next level nodes are at depth 1 and so on.

Example:

Input

Alt text

Output

Alt text

Input format :
Line 1 :  Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Output Format :
 Inorder traversal of modified tree.
Sample Input 1:
     1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 1:
     2 
     1
     2
     0 
     2 
     1
     2

Comments

Popular posts from this blog

Access and modify all the resources of our Wiki.js using WikiJS API

MySQL Multi Source Master Slave Replication using GTID

How to pass parameters in webhook?