快服了我自己了怎么老是把右移节点写成一个l->right没有l=。
/**
* 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 flatten(TreeNode* root) {
if(root==nullptr) return ;
flatten(root->left);
flatten(root->right);
if(root->left){
TreeNode* l=root->left;
while(l->right) l=l->right;
l->right=root->right;
root->right=root->left;
root->left=nullptr;
}
}
};