🧠
Brute Force (Inorder Traversal + For Each Node Sum)
💡 This approach exists to build intuition by directly applying the problem definition: for each node, sum all nodes with greater values. It is simple but inefficient, highlighting the need for better methods.
Intuition
For each node, traverse the entire tree to find and sum all nodes with values greater than the current node's value, then update the node's value.
Algorithm
- Traverse the BST in any order to visit each node.
- For each node, traverse the entire tree again to sum values greater than the current node's value (excluding the node itself).
- Update the current node's value with this sum plus its original value.
- Return the root of the updated tree.
💡 This algorithm is straightforward but involves nested traversals, making it inefficient and hard to scale.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sum_greater(root, val, exclude_node):
if not root:
return 0
total = 0
if root != exclude_node and root.val > val:
total += root.val
total += sum_greater(root.left, val, exclude_node)
total += sum_greater(root.right, val, exclude_node)
return total
def convertBST(root):
if not root:
return None
root.val += sum_greater(root, root.val, root)
convertBST(root.left)
convertBST(root.right)
return root
# Driver code to test
if __name__ == '__main__':
# Construct example tree
root = TreeNode(4,
TreeNode(1,
TreeNode(0),
TreeNode(2, None, TreeNode(3))),
TreeNode(6,
TreeNode(5),
TreeNode(7, None, TreeNode(8))))
new_root = convertBST(root)
# Simple inorder print to verify
def inorder(node):
return inorder(node.left) + [node.val] + inorder(node.right) if node else []
print(inorder(new_root))
Line Notes
class TreeNode:Defines the tree node structure with value and left/right children
def sum_greater(root, val, exclude_node):Helper function to sum nodes with values greater than val excluding the current node
if root != exclude_node and root.val > val:Add node's value only if it is strictly greater than current node's value and not the node itself
root.val += sum_greater(root, root.val, root)Update current node's value by adding sum of greater values excluding itself
convertBST(root.left)Recursively update left subtree nodes
convertBST(root.right)Recursively update right subtree nodes
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
}
public class Solution {
public int sumGreater(TreeNode root, int val, TreeNode excludeNode) {
if (root == null) return 0;
int total = 0;
if (root != excludeNode && root.val > val) total += root.val;
total += sumGreater(root.left, val, excludeNode);
total += sumGreater(root.right, val, excludeNode);
return total;
}
public TreeNode convertBST(TreeNode root) {
if (root == null) return null;
root.val += sumGreater(root, root.val, root);
convertBST(root.left);
convertBST(root.right);
return root;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(1);
root.right = new TreeNode(6);
root.left.left = new TreeNode(0);
root.left.right = new TreeNode(2);
root.left.right.right = new TreeNode(3);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(7);
root.right.right.right = new TreeNode(8);
Solution sol = new Solution();
TreeNode newRoot = sol.convertBST(root);
// Inorder print
inorderPrint(newRoot);
}
static void inorderPrint(TreeNode node) {
if (node == null) return;
inorderPrint(node.left);
System.out.print(node.val + " ");
inorderPrint(node.right);
}
}
Line Notes
class TreeNode {Defines the tree node structure with value and left/right children
public int sumGreater(TreeNode root, int val, TreeNode excludeNode)Helper method to sum nodes with values greater than val excluding the current node
if (root != excludeNode && root.val > val) total += root.val;Add node's value only if greater than current node's value and not the node itself
root.val += sumGreater(root, root.val, root);Update current node's value by adding sum of greater values excluding itself
convertBST(root.left);Recursively update left subtree
convertBST(root.right);Recursively update right subtree
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int sumGreater(TreeNode* root, int val, TreeNode* excludeNode) {
if (!root) return 0;
int total = 0;
if (root != excludeNode && root->val > val) total += root->val;
total += sumGreater(root->left, val, excludeNode);
total += sumGreater(root->right, val, excludeNode);
return total;
}
TreeNode* convertBST(TreeNode* root) {
if (!root) return NULL;
root->val += sumGreater(root, root->val, root);
convertBST(root->left);
convertBST(root->right);
return root;
}
void inorderPrint(TreeNode* root) {
if (!root) return;
inorderPrint(root->left);
cout << root->val << " ";
inorderPrint(root->right);
}
int main() {
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(1);
root->right = new TreeNode(6);
root->left->left = new TreeNode(0);
root->left->right = new TreeNode(2);
root->left->right->right = new TreeNode(3);
root->right->left = new TreeNode(5);
root->right->right = new TreeNode(7);
root->right->right->right = new TreeNode(8);
TreeNode* newRoot = convertBST(root);
inorderPrint(newRoot);
cout << endl;
return 0;
}
Line Notes
struct TreeNode {Defines the tree node structure with value and left/right children
int sumGreater(TreeNode* root, int val, TreeNode* excludeNode)Helper function to sum nodes with values greater than val excluding the current node
if (root != excludeNode && root->val > val) total += root->val;Add node's value only if greater than current node's value and not the node itself
root->val += sumGreater(root, root->val, root);Update current node's value by adding sum of greater values excluding itself
convertBST(root->left);Recursively update left subtree
convertBST(root->right);Recursively update right subtree
class TreeNode {
constructor(val=0, left=null, right=null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function sumGreater(root, val, excludeNode) {
if (!root) return 0;
let total = 0;
if (root !== excludeNode && root.val > val) total += root.val;
total += sumGreater(root.left, val, excludeNode);
total += sumGreater(root.right, val, excludeNode);
return total;
}
function convertBST(root) {
if (!root) return null;
root.val += sumGreater(root, root.val, root);
convertBST(root.left);
convertBST(root.right);
return root;
}
// Driver code
const root = new TreeNode(4,
new TreeNode(1,
new TreeNode(0),
new TreeNode(2, null, new TreeNode(3))),
new TreeNode(6,
new TreeNode(5),
new TreeNode(7, null, new TreeNode(8)))
);
const newRoot = convertBST(root);
function inorder(node) {
if (!node) return [];
return [...inorder(node.left), node.val, ...inorder(node.right)];
}
console.log(inorder(newRoot));
Line Notes
class TreeNode {Defines the tree node structure with value and left/right children
function sumGreater(root, val, excludeNode)Helper function to sum nodes with values greater than val excluding the current node
if (root !== excludeNode && root.val > val) total += root.val;Add node's value only if greater than current node's value and not the node itself
root.val += sumGreater(root, root.val, root);Update current node's value by adding sum of greater values excluding itself
convertBST(root.left);Recursively update left subtree
convertBST(root.right);Recursively update right subtree
TimeO(n^2)
SpaceO(n) due to recursion stack
For each of the n nodes, we traverse the entire tree (n nodes) to sum greater values, resulting in O(n^2) time. Space is O(n) due to recursion depth in worst case.
💡 For n=20, this means about 400 operations, which is inefficient for large trees.
Interview Verdict: TLE / Use only to introduce
This approach is too slow for large inputs but helps understand the problem and motivates better solutions.