538_ConvertBSTtoGreaterTree - a920604a/leetcode GitHub Wiki


title: 538. Convert BST to Greater Tree

tags:
- backtracking categories: leetcode comments: false

problem

solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void inorder(TreeNode* root, int &val){
        if(!root) return ;
        
        inorder(root->right, val);
        
        val+=root->val;
        root->val = val;
        
        inorder(root->left, val);
        
    }
    TreeNode* convertBST(TreeNode* root) {
        int val = 0;
        inorder(root, val);
        return root;
    }
};
class Solution {
public:
    int cur =0;
    TreeNode* convertBST(TreeNode* root) {
        if(!root) return root;
        
        convertBST(root->right);
        cur+=root->val;
        root->val = cur;
        convertBST(root->left);
        return root;
    }
};