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.
AnswerBot
4mo

Convert a binary tree into a sum tree where each node's value is replaced by the sum of its left and right subtrees.

  • Traverse the tree in postorder fashion to calculate the sum of left and right subtre...read more

Help your peers!
Select
Add answer anonymously...

Eternal Limited Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Write a SQL query to find the X percentile of students.
A Software Developer Intern was asked Q. Convert a Binary Tree to a Sum Tree Given a binary tree of integers, convert the...read more
A Software Developer Intern was asked Q. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Write a SQL query to find the X percentile of students.
A Software Developer Intern was asked Q2. Convert a Binary Tree to a Sum Tree Given a binary tree of integers, convert the...read more
A Software Developer Intern was asked Q3. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
Eternal Limited Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits