Triple Sum

It was Todd’s Birthday, So Bojack decided to give Todd a Binary tree with ‘N’ nodes. But the binary tree was too big to keep in his house, so Bojack decided to give exactly three nodes from that tree such that the sum of these three numbers equals ‘X’. Can you help Bojack determine if it is possible to make a sum equal to ‘X’?

For Example :
X = 11

The sum of nodes 7, 3, 1 is 11. So we will return True.
Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases.

The first line of each test case contains the elements of the binary tree in the level order form separated by a single space.

The second line of the test case contains an integer denoting ‘X’.

If any node does not have a left or right child, take -1 in its place. Refer to the example below.

1
2 3
-1 4 -1 -1
5 -1
-1 -1

Explanation :
Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = null (-1)
Right child of 2 = 4
Left child of 3 = null (-1)
Right child of 3 = null (-1)

Level 4 :
Left child of 4 = 5
Right child of 4 = null (-1)

Level 5 :
Left child of 5 = null (-1)
Right child of 5 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).

Note: The above format was just to provide clarity on how the input is formed for a given tree. 

The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 -1 4 -1 -1 5 -1 -1 -1
Output Format :
For each test case, print a boolean True or False.

Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
Constraints :
1 <= T <= 100
1 <= N <= 3000
1 <= NodeVal <= 1000
1 <= X <= 1000

Time Limit: 1sec
AnswerBot
1y

The task is to determine if it is possible to select three nodes from a binary tree such that their sum equals a given value.

  • Traverse the binary tree and store all the node values in an array

  • Use three...read more

CodingNinjas
author
2y

Step1. Implemented brute force solution first.
Step2. Then saw as order of elements does not mattered so thought of sorting it and to use 2 pointer 
Step3. Implemented it.

CodingNinjas
author
2y
Naive Approach

The Approach is simple. We will find the value of every node using dfs and then increase the count of every value. After completing dfs, We will loop through the list and, for a given va...read more

CodingNinjas
author
2y
Optimal Approach

In this approach, we will find the value of every node using dfs and then increase the count of every value. After completing dfs, We will loop through the list and select a value and ...read more

Add answer anonymously...
Jio Software Developer 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
Get AmbitionBox app

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