JavaScript Program to Transform a BST to Greater Sum Tree
Last Updated :
15 Apr, 2025
Given a BST, transform it into a greater sum tree where each node contains the sum of all nodes greater than that node.

Using iterative approach
In this approach we start with the rightmost node of the BST and initialize the sum to 0. We use a stack to perform iterative reverse inorder traversal. At each node, we update its value with the sum of all greater values and then update the sum with the current node's value. We continue moving to the left child if it exists.
Example: Implementation of program to Transform a BST to greater sum tree using Iterative approach.
JavaScript
class Node {
constructor(item) {
this.data = item;
this.left = null;
this.right = null;
}
}
function transform(root) {
let sum = 0;
const stack = [];
let current = root;
while (current || stack.length > 0) {
while (current) {
stack.push(current);
current = current.right;
}
current = stack.pop();
sum += current.data;
current.data = sum - current.data;
current = current.left;
}
}
function inorder(root) {
if (root == null)
return;
inorder(root.left);
console.log(root.data + " ");
inorder(root.right);
}
let Root = new Node(11);
Root.left = new Node(2);
Root.right = new Node(29);
Root.left.left = new Node(1);
Root.left.right = new Node(7);
Root.right.left = new Node(15);
Root.right.right = new Node(40);
Root.right.right.left = new Node(35);
console.log("Inorder Traversal of given tree:");
inorder(Root);
transform(Root);
console.log("\nInorder Traversal of transformed tree:");
inorder(Root);
OutputInorder Traversal of given tree:
1
2
7
11
15
29
35
40
Inorder Traversal of transformed tree:
139
137
130
119
104
75
40
0
Time Complexity: O(N), where N is the number of nodes in the BST.
Auxiliary Space: O(H), where H is the height of the BST.
In this approach we traverse BST in reverse inorder recursively. At each node, we update its value with the sum of all greater values and store the old sum in the current node. We then recur for the left subtree.
Example: Implementation of program to Transform a BST to greater sum tree using recursion.
JavaScript
class Node {
constructor(item) {
this.data = item;
this.left = null;
this.right = null;
}
}
let sum = 0;
let Root;
function transformUtil(root) {
if (root == null)
return;
transformUtil(root.right);
sum = sum + root.data;
root.data = sum - root.data;
transformUtil(root.left);
}
function transform(root) {
transformUtil(root);
}
function inorder(root) {
if (root == null)
return;
inorder(root.left);
console.log(root.data + " ");
inorder(root.right);
}
Root = new Node(11);
Root.left = new Node(2);
Root.right = new Node(29);
Root.left.left = new Node(1);
Root.left.right = new Node(7);
Root.right.left = new Node(15);
Root.right.right = new Node(40);
Root.right.right.left = new Node(35);
console.log("Inorder Traversal of given tree:");
inorder(Root);
transform(Root);
console.log("\nInorder Traversal of transformed tree:");
inorder(Root);
OutputInorder Traversal of given tree:
1
2
7
11
15
29
35
40
Inorder Traversal of transformed tree:
139
137
130
119
104
75
40
0
Time Complexity: O(n) where n is the number of nodes in given Binary Tree, as it does a simple traversal of the tree.
Auxiliary Space: O(h) where h is the height of given Binary Tree due to Recursion
Similar Reads
JavaScript Program to Convert Sorted Array to BST Given a sorted array, convert it into a binary search tree (BST). A binary search tree is a binary tree where the value of each node is larger than the values in its left subtree and smaller than the values in its right subtree. Example: Input: sorted array= [2, 4, 7, 9, 10]Output: Binary Search Tre
3 min read
JavaScript Program to Find Sum of Leaf Node in Binary Tree Given a binary tree, We have to find the sum of all leaf nodes. A leaf node is a node that does not have any children. Example: Input binary tree: 1 / \ 2 3 / \ / \ 4 5 6 7The sum of leaf nodes would be: 4 + 5 + 6 + 7 = 22Table of Content Using recursive approachIterative Approach Using StackUsing r
3 min read
Java Program to Perform the Inorder Tree Traversal The binary tree is the hierarchical data structure in which each node has at most two children and it can referred to as the left child and the right child. Inorder tree traversal is one of the fundamental ways to visit all the nodes in the binary tree. It can specifically visiting the nodes in the
4 min read
Java Program to Construct a Binary Search Tree Binary Search Tree (BST) is the widely used data structure in computer science, primarily known for the efficient search, insertion, and deletion operations. It is the type of binary tree where each node has at most two children, referred to as the left child and the right child. Binary Search Tree
6 min read
Java Program to Implement B+ Tree The B+ tree is a self-balancing tree data structure commonly used in database and file systems applications. It is an extension of B-Tree and maintains sorted data in a manner that allows us for efficient insertion, deletion and search operations. The B+Trees stores all data in a leaf node while int
6 min read
Java Program to Perform the Postorder Tree Traversal The Binary Tree can consist of nodes where each node contains the value and the references to its left and right children. The structure is organized hierarchically with a single root node at the top and each node having at Most Two Children. In this article, we will learn to perform Postorder Tree
3 min read