From be65903f540a913bbda74ab32a9644da3d024364 Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Mon, 10 Jun 2019 23:51:58 +0800 Subject: [PATCH] add 114 --- ...114. Flatten Binary Tree to Linked List.md | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 solutions/114. Flatten Binary Tree to Linked List.md diff --git a/solutions/114. Flatten Binary Tree to Linked List.md b/solutions/114. Flatten Binary Tree to Linked List.md new file mode 100644 index 0000000..fbf74ce --- /dev/null +++ b/solutions/114. Flatten Binary Tree to Linked List.md @@ -0,0 +1,76 @@ +# [114. Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) + +# 思路 +要求把二叉树展开成链表,根据例子可以看到是先序遍历的顺序。 +## 思路一 +最容易想到的就是先序遍历一遍树。我们可以用一个全局的指针`list_last`记录当前链表的最后一个节点,每遍历到一个节点就将其接到`list_last`后面(右指针)。 + +## 思路二 +此题也可以直接递归来做。即如果root的左右子树都已经是被展开好的,那么只需要将左子树接在root的右边,再将右子树接在原来左子树的最下边就可以了,如下例。 +``` + 1 1 + / \ \ +2 5 ===> 2 + \ \ \ + 3 6 3 + \ \ + 4 4 + \ + 5 + \ + 6 +``` + +# C++ +## 思路一 +``` C++ +class Solution { +private: + TreeNode *list_last = NULL; + + void helper(TreeNode *root){ // 先序遍历 + if(root == NULL) return; + if(list_last != NULL) // 不是第一个 + list_last -> right = root; + + list_last = root; + + // 由于后面要将左右指针置空,所以这里先记录一下 + TreeNode *left = root -> left; + TreeNode *right = root -> right; + root -> left = NULL; + root -> right = NULL; + + helper(left); + helper(right); + + } +public: + void flatten(TreeNode* root) { + if(root == NULL) return; + list_last = NULL; + helper(root); + } +}; +``` + +## 思路二 +``` C++ +class Solution { +public: + void flatten(TreeNode* root) { + if(root == NULL) return; // 递归出口 + flatten(root -> left); + flatten(root -> right); + + TreeNode *right = root -> right; // 先备份右子树 + root -> right = root -> left; // 将左子树放在右边 + root -> left = NULL; + + TreeNode *tmp = root; + // 一路下降到最下面 + while(tmp -> right) tmp = tmp -> right; + tmp -> right = right; + } +}; +``` \ No newline at end of file