Binary Tree K-Sum Paths Problem

Given a binary tree where each node contains an integer value, and a number 'K', your task is to find and output all paths in the tree where the sum of the node values equals 'K'. The paths must travel downwards from parent nodes to child nodes.

Input:

The input starts with an integer 'T' denoting the number of test cases. For each test case, two lines follow:
The first line presents elements of the tree in level order, separated by a space. Use -1 for any null node.
The second line contains an integer 'K', representing the target path sum.

Output:

For each test case, output all paths where node values sum up to 'K'. Print paths in the order they appear in the tree, left to right, one per line.

Example:

Example for K = 4:
Input Tree: 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
K: 4
Output Paths:
1 3
3 1
-1 4 1
4
-1 5

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 102
  • -109 <= K <= 109
  • -107 <= DATA <= 107

Where 'N' represents the number of nodes, 'K' is the path sum to find, and 'DATA' is the value at each tree node.

Note:

You do not need to print anything directly. Implement the function to return the correct paths. Paths should be identified as sequences of node values, respecting the tree's left-to-right order.

AnswerBot
4mo

Find all paths in a binary tree where the sum of node values equals a given number 'K'.

  • Traverse the binary tree in a depth-first manner while keeping track of the current path and sum of node values.

  • W...read more

Help your peers!
Select
Add answer anonymously...

UST Software Engineer interview questions & answers

A Software Engineer was asked 8mo agoQ. What is Criteria in Hibernate?
A Software Engineer was asked 8mo agoQ. How do you create a custom serializable interface?
A Software Engineer was asked Q. What is the event loop in JavaScript?

Popular interview questions of Software Engineer

A Software Engineer was asked 8mo agoQ1. What is Criteria in Hibernate?
A Software Engineer was asked 8mo agoQ2. How do you create a custom serializable interface?
A Software Engineer was asked Q3. Write a program to check if a string is a palindrome.

Top HR questions asked in UST Software Engineer

A Software Engineer was asked 6mo agoQ1. Can you explain your projects to me?
A Software Engineer was asked Q2. Tell me a little about yourself.
A Software Engineer was asked Q3. What do you like about your current role in the project?
UST Software Engineer 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