Replace Node With Depth Problem Statement

For a given Binary Tree of integers, replace each of its data with the depth of the tree. The 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.

Input:

The first line contains an integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case contains the elements of the tree in level order form separated by a single space. If any node does not have a left or right child, take -1 in its place.

Output:

For each test case, print the inorder traversal of the tree where each node is replaced by the depth of the current node. Output for every test case will be denoted in a separate line.

Example:

Input: 
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output: 
2 1 2 0 2 1 2

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000
  • -10^9 <= data <= 10^9
  • Time limit: 1 sec
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter