update 114 and add 114 to readme

This commit is contained in:
ShusenTang 2019-06-11 19:56:58 +08:00
parent be65903f54
commit 0ad5b9e56a
2 changed files with 61 additions and 1 deletions

View File

@ -93,6 +93,7 @@ LeetCode solutions with Chinese explanation. LeetCode中文题解。
| 111 |[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/111.%20Minimum%20Depth%20of%20Binary%20Tree.md)|Easy| | | 111 |[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/111.%20Minimum%20Depth%20of%20Binary%20Tree.md)|Easy| |
| 112 |[Path Sum](https://leetcode.com/problems/path-sum)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/112.%20Path%20Sum.md)|Easy| | | 112 |[Path Sum](https://leetcode.com/problems/path-sum)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/112.%20Path%20Sum.md)|Easy| |
| 113 |[Path Sum II](https://leetcode.com/problems/path-sum-ii/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/113.%20Path%20Sum%20II.md)|Medium| | | 113 |[Path Sum II](https://leetcode.com/problems/path-sum-ii/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/113.%20Path%20Sum%20II.md)|Medium| |
| 114 |[Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/114.%20Flatten%20Binary%20Tree%20to%20Linked%20List.md)|Medium| |
| 118 |[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/118.%20Pascal's%20Triangle.md)|Easy| | | 118 |[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/118.%20Pascal's%20Triangle.md)|Easy| |
| 119 |[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/119.%20Pascal's%20Triangle%20II.md)|Easy| | | 119 |[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/119.%20Pascal's%20Triangle%20II.md)|Easy| |
| 121 |[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock.md)|Easy| | | 121 |[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock.md)|Easy| |

View File

@ -5,7 +5,7 @@
## 思路一 ## 思路一
最容易想到的就是先序遍历一遍树。我们可以用一个全局的指针`list_last`记录当前链表的最后一个节点,每遍历到一个节点就将其接到`list_last`后面(右指针)。 最容易想到的就是先序遍历一遍树。我们可以用一个全局的指针`list_last`记录当前链表的最后一个节点,每遍历到一个节点就将其接到`list_last`后面(右指针)。
## 思路二 ## 思路二*
此题也可以直接递归来做。即如果root的左右子树都已经是被展开好的那么只需要将左子树接在root的右边再将右子树接在原来左子树的最下边就可以了如下例。 此题也可以直接递归来做。即如果root的左右子树都已经是被展开好的那么只需要将左子树接在root的右边再将右子树接在原来左子树的最下边就可以了如下例。
``` ```
1 1 1 1
@ -21,6 +21,42 @@
6 6
``` ```
## 思路三*
可以将思路二改成非递归版本。原理类似当前工作指针p指向root如果p有左子树那么将其左子树接在p的右边而原来的右子树接在左子树的最右下边节点此为一次循环然后工作指针p向右下移动一步重复上述操作。下面举个例子
```
原二叉树:
1
/ \
2 5
/ \ \
3 4 6
第一次工作指针p指向1循环后:
1
\
2
/ \
3 4
\
5
\
6
第二次工作指针p指向2循环后:
1
\
2
\
3
\
4
\
5
\
6
接下来每次循环p依次指向3、4、5、6由于p没有左子树所以不作任何操作。
```
# C++ # C++
## 思路一 ## 思路一
``` C++ ``` C++
@ -73,4 +109,27 @@ public:
tmp -> right = right; tmp -> right = right;
} }
}; };
```
## 思路三
``` C++
class Solution {
public:
void flatten(TreeNode* root) {
TreeNode *p = root;
while(p){
if(p -> left){
// 先备份右子树
TreeNode *right_bk = p -> right;
p -> right = p -> left;
p -> left = NULL;
TreeNode *tmp = p;
while(tmp -> right) tmp = tmp -> right;
// 此时tmp指向p原来左子树的最右下节点
tmp -> right = right_bk;
}
p = p -> right;
}
}
};
``` ```