897. 递增顺序搜索树 - 力扣(LeetCode) (leetcode-cn.com)
太巧妙了吧,只需一个全局变量current。。。。
class Solution {
TreeNode *current;
public:
TreeNode* increasingBST(TreeNode* root) {
TreeNode *head=new TreeNode(0);
current=head;
fun(root);
return head->right;
}
void fun(TreeNode*root){
if(root==nullptr) return;
fun(root->left);
current->right=new TreeNode(root->val);
current=current->right;
fun(root->right);
}
};