Binary Search Tree to Greater Sum Tree - Complete Solution Guide
Binary Search Tree to Greater Sum Tree is LeetCode problem 1038, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must als
Detailed Explanation
The problem requires us to convert a given Binary Search Tree (BST) into a Greater Sum Tree (GST). In a GST, each node's value is the sum of all nodes greater than or equal to it in the original BST. This means for each node, we need to add the values of all nodes to its right (larger values) and its own original value to get its new value.
Solution Approach
The provided solution uses a reverse inorder traversal of the BST. We maintain a `current_sum` variable that keeps track of the sum of all nodes visited so far in the reverse inorder. During the traversal, for each node, we add its original value to the `current_sum`, update the node's value to be the new `current_sum`, and then continue the traversal to the left subtree. This process effectively converts the BST to a GST.
Step-by-Step Algorithm
- Step 1: Initialize a variable `current_sum` to 0. This will store the running sum of node values encountered during the reverse inorder traversal.
- Step 2: Define a recursive function `reverse_inorder(node)` that takes a node as input.
- Step 3: In the `reverse_inorder` function, check if the node is null. If it is, return (base case).
- Step 4: Recursively call `reverse_inorder` on the right child of the current node. This ensures that we visit the largest values first.
- Step 5: Add the current node's value to the `current_sum`.
- Step 6: Update the current node's value to be the `current_sum`.
- Step 7: Recursively call `reverse_inorder` on the left child of the current node.
- Step 8: Call the `reverse_inorder` function with the root node to start the traversal.
- Step 9: Return the modified root node.
Key Insights
- Insight 1: The key insight is recognizing that a reverse inorder traversal of the BST allows us to visit the nodes in descending order.
- Insight 2: Using a running sum allows us to efficiently accumulate the values of the larger nodes as we traverse the tree in reverse inorder.
- Insight 3: The BST property guarantees that the right subtree contains larger values, which is crucial for the reverse inorder traversal.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(h)
Topics
This problem involves: Tree, Depth-First Search, Binary Search Tree, Binary Tree.
Companies
Asked at: Datadog, SAP, eBay.