Convert a Binary Tree to a Sum Tree

Given a binary tree of integers, convert the binary tree into a sum tree where each node's value is replaced by the sum of the values of its left and right subtrees in the original tree. The value of leaf nodes should be set to zero.

Input:

1. An integer T representing the number of test cases.
2. For each test case, a line with level order traversal of the binary tree with node values separated by a space. Use -1 to denote null nodes. -1 is not included in actual tree node values.

Output:

For each test case, produce a line of level order traversal of the sum tree with node values separated by spaces, using -1 to represent null nodes.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
[output would depend on internal calculations of the sum tree] 

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • -104 <= val <= 104 and val != -1
Note:
The binary tree is passed in level order format, using -1 for null nodes. Actual node values will never be -1. Implement the function and return the result, printing is handled separately.
Be the first one to answer
Add answer anonymously...
Zomato 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