Maximum Path Sum Problem Statement

You are given an n-ary tree consisting of 'N' nodes. Your task is to determine the maximum sum of the path from the root to any leaf node.

Example:

Input:
For the given tree:

The path 1 -> 3 -> 7 produces the maximum, i.e., 11.

Input:

The first line of the input contains a single integer 'T', representing the number of test cases.

The first line of each test case contains an integer 'N', which denotes the number of nodes in the tree.

The second line of each test case contains elements of the N-ary tree in level order. The values of nodes are separated by a single space. If a node is absent, it is represented by -1. The first non-null node (of the previous level) acts as the parent of the first node in the current level. Similarly, the second non-null node (of the previous level) acts as the parent of the subsequent nodes of the current level, and so forth.

Output:

For each test case, print the maximum sum of the path from the root to the leaf node.

Print the output of each test case in a separate line.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5000
  • 0 <= DATA <= 104

Where 'T' is the number of test cases, 'N' is the total number of nodes in the binary tree, and 'DATA' is the value of the tree node.

Time Limit: 1 sec

AnswerBot
4mo

Find the maximum sum of the path from the root to any leaf node in an n-ary tree.

  • Traverse the tree from root to leaf nodes while keeping track of the sum of each path.

  • At each node, calculate the sum o...read more

Help your peers!
Select
Add answer anonymously...

Ansys Software Private Limited Software Developer interview questions & answers

A Software Developer was asked 12mo agoQ. Draw the recursion tree for this code.
A Software Developer was asked Q. Explain how indexing in a database works using B+ trees.
A Software Developer was asked Q. Binary Tree Right View Problem Statement Given a binary tree of integers, your t...read more

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. Draw the recursion tree for this code.
A Software Developer was asked Q2. Explain how indexing in a database works using B+ trees.
A Software Developer was asked Q3. Binary Tree Right View Problem Statement Given a binary tree of integers, your t...read more
Ansys Software Private Limited Software Developer 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