1038. Binary Search Tree to Greater Sum Tree 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526272829303132/** * 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* bstToGst(TreeNode* root) { int val = 0; inorder(root, val); return root; }};