1038. Binary Search Tree to Greater Sum Tree Posted on 2021-01-17 Edited on 2021-01-25 In LeetCode Disqus: Symbols count in article: 407 Reading time ≈ 1 mins. 右中左序遍历,维护一个全局的sum 123456789101112131415161718192021/** * 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: TreeNode* bstToGst(TreeNode* root) { if (!root) return root; bstToGst(root->right); root->val = sum += root->val; bstToGst(root->left); return root; } int sum = 0;};