1. 题目
2. 解题
- 二叉搜索树 逆中序遍历(右根左)是降序的
class Solution {
public:TreeNode* bstToGst(TreeNode* root) {stack<TreeNode*> stk;int sum = 0;TreeNode *rootcopy = root;while(root || !stk.empty()) {while(root){stk.push(root);root = root->right;}sum += stk.top()->val;stk.top()->val = sum;root = stk.top()->left;stk.pop();}return rootcopy;}
};