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.
AnswerBot
4mo

Convert a given BST into a Greater Tree by updating each node's data to the sum of original data and all greater nodes' data.

  • Traverse the BST in reverse inorder (right, root, left) to update nodes' da...read more

Help your peers!
Select
Add answer anonymously...

Oracle Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. BST to Greater Tree Conversion Convert a given binary search tree (BST) with 'N'...read more
A Software Developer Intern was asked Q. Merge K Sorted Arrays Problem Statement Given 'K' different arrays that are indi...read more
A Software Developer Intern was asked Q. Find The Repeating And Missing Number Problem Statement You are provided with an...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. BST to Greater Tree Conversion Convert a given binary search tree (BST) with 'N'...read more
A Software Developer Intern was asked Q2. Merge K Sorted Arrays Problem Statement Given 'K' different arrays that are indi...read more
A Software Developer Intern was asked Q3. Find The Repeating And Missing Number Problem Statement You are provided with an...read more
Oracle 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