Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 1.35 KB

_1038. Binary Search Tree to Greater Sum Tree.md

File metadata and controls

58 lines (42 loc) · 1.35 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 26, 2024

Last updated : June 26, 2024


Related Topics : Tree, Depth-First Search, Binary Search Tree, Binary Tree

Acceptance Rate : 88.3 %


Solutions

C

// Did this like 3 days before it was a daily question so I just had to resubmit for the daily lol

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int adjustNodeValues(struct TreeNode* root, int addVal) {
    int add = root->val + addVal;
    if (root->right) {
        add += adjustNodeValues(root->right, addVal) - addVal;
    }

    root->val = add;

    if (root->left) {
        add += adjustNodeValues(root->left, add) - add;
    }

    return add;
}

struct TreeNode* bstToGst(struct TreeNode* root) {
    adjustNodeValues(root, 0);
    return root;
}