解题思路
从局部看整体,二叉搜索树中序有序,只要从中序遍历中转换就可以,完成一个结点得转换,再不断递归完成整颗树得转换
class Solution {
public:
TreeNode* prev = NULL;
TreeNode* first;
void fun(TreeNode* node)
{
node->left = prev;
if(prev!=NULL)
prev->right = node;
else
first = node;
prev = node;
}
void Inorder(TreeNode* root)
{
if(root)
{
Inorder(root->left);
fun(root);
Inorder(root->right);
}
}
TreeNode* convert(TreeNode* root) {
first = NULL;
Inorder(root);
return first;
}
};