/** * 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 dfs(TreeNode* T, TreeNode* &pre, TreeNode* &NT) { if(T == nullptr) return; dfs(T->left, pre, NT); if(pre != nullptr) pre->right = T; else NT = T; T->left = nullptr; pre = T; dfs(T->right, pre, NT); } TreeNode* increasingBST(TreeNode* root) { TreeNode* pre = nullptr; TreeNode* NT = nullptr; dfs(root, pre, NT); pre->right = nullptr; return NT; } };