[LeetCode]538. Convert BST to Greater Tree
题目描述
思路
实际上是二叉树的中序遍历,顺序为 右-中-左
代码
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void travel(TreeNode* root) {
if (root == NULL) return;
if (root->right) travel(root->right);
curSum += root->val;
root->val = curSum;
if (root->left) travel(root->left);
}
TreeNode* convertBST(TreeNode* root) {
travel(root);
}
private:
int curSum = 0;
};