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
4d

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!
Add answer anonymously...
Adobe Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter