K - Sum Path In A Binary Tree

Given a binary tree where each node contains an integer value, and a value 'K', your task is to find all the paths in the binary tree such that the sum of the node values in each path equals 'K'.

Example:

Input:
tree = [1, 2, 3, 4, -1, 5, 6, -1, 7, -1, -1, -1, -1, -1, -1]
K = 4
Output:
[ [1, 3], [3, 1], [-1, 4, 1], [4], [-1, 5] ]
Explanation:
tree graphic

[1, 3], [3, 1], [-1, 4, 1], [4], and [-1, 5] are paths where the sum of values is 4.

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 10^2
  • -10^9 ≤ K ≤ 10^9
  • -10^7 ≤ DATA ≤ 10^7
Note:
The path can start and end anywhere, but it must move downwards (from parent to child). Implement the function and return paths in order; you don't need to print.
AnswerBot
4mo

Find all paths in a binary tree with nodes summing up to a given value K.

  • Traverse the binary tree and keep track of the current path and its sum.

  • At each node, check if the sum equals K and store the p...read more

Help your peers!
Select
Add answer anonymously...

Nagarro Software Developer interview questions & answers

A Software Developer was asked 3mo agoQ. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 3mo agoQ. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
A Software Developer was asked 5mo agoQ. What is abstraction in Java?

Popular interview questions of Software Developer

A Software Developer was asked 3mo agoQ1. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 3mo agoQ2. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
A Software Developer was asked 5mo agoQ3. What is abstraction in Java?
Nagarro 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