Update appNew update is available. Click here to update.

Replace Node With Depth

Contributed by
Dhruv Sharma
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 20 mins
Success Rate 85 %
Share
7 upvotes

Problem Statement

For a given Binary Tree of integers, replace each of its data with the depth of the tree.

Root is at depth 0, hence the root data is updated with 0. Replicate the same further going down the tree in the depth of the given tree.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 100
1 <= N <= 1000
-10^9 <= data <= 10^9

Time limit: 1 sec
Sample Input 1 :
1
2 35 10 2 3 5 2 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 1 :
2 1 2 0 2 1 2
Explanation Of the Sample Input 1 :

Sample Input 1

Sample Output 1

Sample Input 2 :
1
1 2 -1 3 -1 4 -1 5 -1 -1 -1
Sample Output 2 :
4 3 2 1 0
Explanation Of the Sample Input 2 :

Input 2

Output 2

Reset Code
Full screen
Auto
copy-code
Console