
Asked in ThoughtWorks
Boundary Sum Problem Statement
Given a Binary Tree with 'N' nodes, compute and return the sum of all its boundary nodes. The boundary nodes include the root node, leftmost external nodes, rightmost external nodes, and all the leaves of the tree.
Explanation:
In the provided binary tree example, we have the following boundary nodes:
- Root: 2
- Leftmost: 35
- Rightmost: 10
- Leaf nodes: 2, 3, 5, 2
The total sum of these nodes would be 59.
Input:
The first line contains an integer 'T' denoting the number of test cases. Each test case consists of a single line containing the elements of the tree in level order, separated by a space. Use -1 for nonexistent children.
Output:
For each test case, output a single integer representing the sum of the boundary nodes. Each output should be on a new line.
Example:
Tree nodes in level order:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
For this tree, the boundary nodes are formed as follows:
- Root: 1
- Leftmost: 2
- Rightmost: 3
- Leaf nodes: 4, 5, 6, 7
Boundary sum would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
Constraints:
- 1 <= T <= 100
- 1 <= N <= 1000
- -106 <= node.data <= 106
- Each node data value is bounded within the range from -106 to 106.
- Time Limit: 1 second
Note:
You do not need to print anything explicitly. Just implement the function as required.

AnswerBot
4mo
The Boundary Sum Problem involves calculating the sum of boundary nodes in a binary tree.
Traverse the tree in a specific order to identify the boundary nodes (root, leftmost, rightmost, and leaves)
Kee...read more
Help your peers!
Add answer anonymously...
Top Software Engineer Interview Questions Asked at ThoughtWorks
Q. Which tech stack would you use for which case?
Q. Describe a real-time situation and how you would code it using OOP concepts.
Q. Write a program to print the HCF of numbers as output.
Interview Questions Asked to Software Engineer at Other Companies
Top Skill-Based Questions for ThoughtWorks Software Engineer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

