K-Sum Path in a Binary Tree Problem Statement

You are presented with a binary tree where each node holds an integer, along with a specified number 'K'. The task is to identify and print every path that exists within the binary tree whose node values sum up to 'K'. These paths should traverse downwards, moving from parent nodes to child nodes.

Input:

The first line contains an integer 'T', indicating the number of test cases.
The subsequent 'T' lines present the test cases.
Each test case starts with a line containing the tree's elements in level order with spaces separating them.
If a node lacks a left or right child, use -1 in its place.
The succeeding line for each test case comprises a single integer 'K' for the desired sum.

Output:

Print each valid path in the binary tree for every test case, ensuring the order of paths proceeds from left to right as encountered in the tree.
Each test case should have paths displayed on a separate line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
4
Output:
The potential paths comprising a sum of 4 include:
1 3
3 1
-1 4 1
4
-1 5
Explanation:

The binary tree's level-by-level representation:
Level 1: Root node is 1
Level 2: Left child of 1 is 2, Right child is 3
Level 3: Left child of 2 is 4, Right child is none (-1), Left child of 3 is 5, Right child is 6
Levels further continue as specified in the input format.
The specified node paths satisfy the condition that their sum equals 4.

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 102
  • -109 ≤ K ≤ 109
  • -107 ≤ DATA ≤ 107
  • Time Limit: 1 sec
AnswerBot
4mo

The task is to identify and print every path in a binary tree whose node values sum up to a specified number 'K'.

  • Traverse the binary tree to find paths with sum equal to 'K'.

  • Print each valid path in t...read more

Help your peers!
Select
Add answer anonymously...

Adobe Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
A Software Developer Intern was asked Q. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
A Software Developer Intern was asked Q. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
A Software Developer Intern was asked Q2. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
A Software Developer Intern was asked Q3. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more
Adobe 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