538. Convert BST to Greater Tree

1. Description

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 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 also be binary search trees.

2. Note

3. Example

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]

Example 3:
Input: root = [1,0,2]
Output: [3,3,2]

Example 4:
Input: root = [3,2,4,1]
Output: [7,9,4,10]

4. Constraints

  • The number of nodes in the tree is in the range [0, $10^4$].
  • $-10^4$ <= Node.val <= $10^4$
  • All the values in the tree are unique.
  • root is guaranteed to be a valid binary search tree.

5. Solutions

My Accepted Solution

n is the number of nodes in m_root
Time complexity: O(n)
Space complexity: O(n)

class Solution 
{
private:
    vector<TreeNode *> nodesInPreorder;

    void preorderTraverse(TreeNode *i_root)
    {
        if(i_root->left) preorderTraverse(i_root->left);
        
        nodesInPreorder.push_back(i_root);
        
        if(i_root->right) preorderTraverse(i_root->right);        
    } 
    
public:
    // TreeNode* convertBST(TreeNode* root)
    TreeNode* convertBST(TreeNode *m_root) 
    {
        if(!m_root) return nullptr;
        
        preorderTraverse(m_root);
        for(int i = nodesInPreorder.size() - 2; i >= 0; i--)
        {
            nodesInPreorder[i]->val += nodesInPreorder[i+1]->val;
        }
        
        return m_root;
    }
};

5.1 Reverse Preorder Traversal && Iteration

n is the number of nodes in m_root
Time complexity: O(n)
Space complexity: O(n)

class Solution 
{
public:
    // TreeNode* convertBST(TreeNode* root)
    TreeNode* convertBST(TreeNode *m_root) 
    {
        int postSum = 0;
        auto iter = m_root;
        stack<TreeNode *> parentNodes;
        
        while(iter || !parentNodes.empty())
        {
            // find the most right node
            while(iter)
            {
                parentNodes.push(iter);
                iter = iter->right;
            }
            
            iter = parentNodes.top();
            parentNodes.pop();
            
            postSum += iter->val;
            iter->val = postSum;
            
            iter = iter->left;
        }
        
        return m_root;
    }
};

5.2 Reverse Morris && Iteration

n is the number of nodes in m_root
Time complexity: O(n)
Space complexity: O(1)

class Solution 
{
public:
    // TreeNode* convertBST(TreeNode* root)
    TreeNode* convertBST(TreeNode *m_root) 
    {
        int postSum = 0;
        TreeNode *currentNode = m_root, *previousNode = nullptr;
        while(currentNode)
        {
            if(!currentNode->right)
            {
                postSum += currentNode->val;
                currentNode->val = postSum;
                
                currentNode = currentNode->left;
            }
            else
            {
                previousNode = currentNode->right;
                
                while(previousNode->left && previousNode->left != currentNode)
                    previousNode = previousNode->left;
                
                if(!previousNode->left)
                {
                    previousNode->left = currentNode;
                    currentNode = currentNode->right;
                }
                else
                {
                    previousNode->left = nullptr;
                    
                    postSum += currentNode->val;
                    currentNode->val = postSum;
                    
                    currentNode = currentNode->left;
                }
            }
        }
        
        return m_root;
    }
};
Last updated:
Tags: Tree
comments powered by Disqus