这道题的难度定义是easy,但是,乍一看还是被唬住了的感觉,所以看了看别人的思路,其实就是中序遍历的操作加上指针左指针置空,右指针回指,用一个全局的指针去完成整棵树的重新搭建。
直接看代码应该也很容易。
class Solution {
public:
TreeNode* cur = NULL;
TreeNode* increasingBST(TreeNode* root) {
if (!root) {
return NULL;
}
TreeNode* ans = new TreeNode(0);
cur = ans;
inorder(root);
return ans->right;
}
void inorder(TreeNode* node) {
if (!node) {
return ;
}
inorder (node->left);
node->left = NULL;
cur->right = node;
cur = cur->right;
inorder (node->right);
}
};