BST to Greater Tree Conversion

Convert a given binary search tree (BST) with 'N' nodes into a Greater Tree. In this transformation, each node's data is modified to the sum of the original node's data plus the sum of all nodes' data that are greater than or equal to the original node's data within the BST.

Input:

The first line contains an integer 'T' representing the number of test cases.
For each test case, the subsequent line contains the tree elements in level order, separated by a space. Use -1 to denote missing nodes.

Output:

For each test case, output a single integer on a new line, representing the root node of the modified Greater Tree.

Example:

Given the input for level order traversal as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output will be the root node after converting the BST.
Example

Constraints:

  • 1 <= T <= 100
  • 0 <= N <= 3000
  • -10^6 <= DATA <= 10^6
  • Duplicate elements can be present, placed in either left or right subtrees.
  • Time Limit: 1 sec

Note:

You do not have to manage input/output operations. Implement the function to modify the BST.
Be the first one to answer
Add answer anonymously...
Samsung 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