Binary Tree Maximum Path Sum Problem Statement

Determine the maximum path sum for any path in a given binary tree with 'N' nodes.

Note:

  • A 'path' is defined as any sequence of adjacent nodes connected by an edge within the binary tree.
  • The 'path' does not need to pass through the root of the tree.
  • The 'path sum' is summed from the values of nodes along that path.

Input:

The first line contains an integer T which denotes the number of test cases to be processed.
Each test case consists of elements of the binary tree given in level-order form, where node values are separated by a single space, and '-1' signifies a null node.

1 2 3 4 -1 5 6 -1 -1 -1 -1 -1 -1

Output:

For each test case, print a single integer indicating the maximum path sum. Each test case result should be on a new line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 -1 -1 -1 -1 -1
Output:
16
Explanation:

In the given binary tree, one possible maximum path is from node 4 through node 2, node 1, node 3, to node 5 which gives the maximum sum.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 2000
  • -5000 ≤ data ≤ 5000 (data ≠ -1 as -1 denotes null nodes)
  • Where 'N' is the number of nodes, and 'data' is the value of the node.

Time Limit: 1 sec

Implementation Note: You do not need to print anything for the function. The output is managed independently.

AnswerBot
4mo

Find the maximum path sum in a binary tree by considering any path of adjacent nodes.

  • Traverse the binary tree to find the maximum path sum by considering all possible paths.

  • Keep track of the maximum s...read more

Help your peers!
Select
Add answer anonymously...

InterviewBit Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Can you describe the assignment you received to build a website, including all t...read more
A Software Developer Intern was asked Q. Binary Tree Maximum Path Sum Problem Statement Determine the maximum path sum fo...read more
A Software Developer Intern was asked Q. Sum of Two Numbers Represented as Arrays Given two numbers in the form of two ar...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Can you describe the assignment you received to build a website, including all t...read more
A Software Developer Intern was asked Q2. Binary Tree Maximum Path Sum Problem Statement Determine the maximum path sum fo...read more
A Software Developer Intern was asked Q3. Sum of Two Numbers Represented as Arrays Given two numbers in the form of two ar...read more
InterviewBit 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