# LeetCode 114. 二叉樹展開爲鏈表(遞歸、尋找尾結點、鏈表的拼接)

① 將左、右子樹分別轉換爲一條鏈 （相同問題，遞歸解決）。
② 找到左邊這條鏈的尾結點
③左邊的這條鏈的尾結點接上右邊這條鏈的頭結點
④將根節點的右孩子更新爲左邊這條鏈。

``````/**
* 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){
return;
}
flatten(root->left);
flatten(root->right);
if(root->left){
TreeNode* p = root->left;
//找到左邊這條鏈的末尾節點
while(p->right){
p = p->right;
}
p->right = root->right; //先使用後破壞
root->right = root->left;
}
root->left = nullptr;
}
};
``````

``````/**
* 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) {
dfs(root);
}
TreeNode* dfs(TreeNode* root){
if(!root){
return nullptr;
}
//如果root是葉節點,直接返回root
if(!root->left && !root->right){
return root;
}
TreeNode *leftBackNode = dfs(root->left);
TreeNode *rightBackNode =  dfs(root->right);
if(root->left){
leftBackNode->right = root->right;
root->right = root->left;
}
root->left = nullptr;
return rightBackNode?rightBackNode:leftBackNode;
}
};
``````