Skip to content

1038. Binary Search Tree to Greater Sum Tree #339

Open
@namespace-io

Description

@namespace-io

太久没做题了 没手感居然写了半小时

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int prev = 0;
    
    TreeNode* bstToGst(TreeNode* cur) {
 
        if(cur == nullptr) return nullptr;
        auto root = new TreeNode(0);
        root->right = bstToGst(cur->right);
        root->val = cur->val + prev;
        prev = root->val;
        root->left = bstToGst(cur->left);
 
        return root;;
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions