面试题 17.12. BiNode - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
TreeNode* pre=new TreeNode(0);//记得分配内存
public:
TreeNode* convertBiNode(TreeNode* root) {
if(root==nullptr) return nullptr;
TreeNode *temp=pre;
fun(root);
return temp->right;
}
void fun(TreeNode*root){
if(root==nullptr) return;
fun(root->left);
pre->right=root;
root->left=nullptr;
pre=pre->right;
fun(root->right);
}
};