Leetcode 之Flatten Binary Tree to Linked List(50)
将左子树接到右子树之前,递归解决
void flatten(TreeNode *root) { if (root == nullptr)return; flatten(root->left); flatten(root->right); //如果没有左子树,直接返回即可 if (root->left == nullptr)return; p = root->left; //寻找左子树的最后一个结点 while (p->right)p = p->right; //将右结点接在左子树的最后一个结点上 p->right = root->right; //将左子树移到右子树上 root->right = root->left; root->left = nullptr; }