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:
Example 1:
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input: root = [0,null,1] Output: [1,null,1]
Constraints:
[0, 104].-104 <= Node.val <= 104root is guaranteed to be a valid binary search tree.
Note: This question is the same as 1038: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
This approach leverages the property of Binary Search Trees where the nodes in the right subtree are greater than those in the left subtree. By performing a reverse inorder traversal (right -> root -> left), we can accumulate the sum of all nodes greater than the current node and update each node with this accumulated sum. The traversal ensures that we always process the greater nodes first.
The C solution defines a helper function traverse that modifies the tree in place. This function is called with the root of the tree and maintains a running total of the sum of node values. We first recurse into the right subtree (greater values), add the current node's value to the sum, update the node's value to the sum, and finally recurse into the left subtree.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of nodes in the BST, since we visit each node exactly once.
Space Complexity: O(h), where h is the height of the tree, due to the recursion stack used during traversal.
The Morris Traversal technique allows in-order traversal of a binary tree without using extra space for recursion or a stack. It modifies the tree structure during the traversal, and at the end of the traversal, the original tree structure is restored.
For this problem, we adapt the Morris Traversal to traverse the tree in reverse inorder fashion and keep track of the sum of nodes greater than the current node.
This Python solution uses the Morris Traversal technique, which allows modifying the tree without using stack or recursion explicitly. The key idea is to establish a temporary link between each node and its inorder predecessor, traverse in reverse inorder, update the nodes, and then restore the tree structure by removing the temporary links.
Time Complexity: O(n), where n is the number of nodes. Although every edge is visited at most twice, the overall complexity remains linear.
Space Complexity: O(1) since no extra space is used apart from variables.
| Approach | Complexity |
|---|---|
| Reverse Inorder Traversal Approach | Time Complexity: O(n), where n is the number of nodes in the BST, since we visit each node exactly once. |
| Morris Traversal Approach | Time Complexity: O(n), where n is the number of nodes. Although every edge is visited at most twice, the overall complexity remains linear. |
Convert BST to Greater Tree - Leetcode 538 - Python • NeetCode • 25,414 views views
Watch 9 more video solutions →Practice Convert BST to Greater Tree with our built-in code editor and test cases.
Practice on FleetCode