/**
* 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 recursion(TreeNode* process_node ,vector<int>& all_nums)
{
if(process_node->left) recursion(process_node->left,all_nums);
//这一部分是处理代码
all_nums.push_back(process_node->val);
if(process_node->right) recursion(process_node->right,all_nums);
}
void another_recursion(TreeNode* process_node ,vector<int>& sum_vector)
{
if(process_node->left) another_recursion(process_node->left,sum_vector);
//这一部分是处理代码
process_node->val=sum_vector[0];
sum_vector.erase(sum_vector.begin());
if(process_node->right) another_recursion(process_node->right,sum_vector);
}
TreeNode* convertBST(TreeNode* root)
{
int i,j;
vector<int> all_nums;
vector<int> sum_vector;
int sum=0;
if(root!=nullptr)
{
recursion(root,all_nums);
for(i=0;i<all_nums.size();i++) std::cout<<" "<<all_nums[i];
std::cout<<std::endl;
for(i=0;i<all_nums.size();i++) sum+=all_nums[i];
std::cout<<" sum "<<sum <<std::endl;
for(i=0;i<all_nums.size();i++)
{
sum_vector.push_back(sum);
sum-=all_nums[i];
}
for(i=0;i<sum_vector.size();i++) std::cout<<" "<<sum_vector[i];
std::cout<<std::endl;
another_recursion(root,sum_vector);
}
return root;
}
};