add 117
This commit is contained in:
ShusenTang 2019-06-13 23:55:39 +08:00
commit 7287cefddd
2 changed files with 59 additions and 0 deletions

View File

@ -94,6 +94,7 @@ LeetCode solutions with Chinese explanation. LeetCode中文题解。
| 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| | | 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| |
| 116 |[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/116.%20Populating%20Next%20Right%20Pointers%20in%20Each%20Node.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

@ -0,0 +1,58 @@
# [116. Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)
# 思路
题目要求将满二叉树同一层的节点用指针串起来要求空间复杂度O(1)。
## 思路一、递归
最好想的还是递归: 如果root的左右子树都已经串好了的话那么我们只需要将左子树每一层的最右边的节点的next指向右子树对应层最左边节点即可。
## 思路二、非递归
由于next指针将每一层的节点都串起来了所以我们可以很方便地从第一层的最左边节点开始访问第二层的所有节点、从第二层的最左边节点开始访问第三层的所有节点...
因此我们可以用这个思路从上往下依次将每一层串起来。我们用start表示每层的最左边节点如果该层不是最后一层那么我们应该将下一层所有节点串起来。
该方法真正实现了空间复杂度O(1)。
# C++
## 思路一
``` C++
class Solution {
public:
Node* connect(Node* root) {
if(root == NULL) return NULL; // 递归出口
Node *p_left = connect(root -> left); // 左子树都已经串好了
Node *p_right = connect(root -> right); // 右子树都已经串好了
// 将左子树每一层的最右边的节点的next指向右子树对应层最左边节点
while(p_left){
p_left -> next = p_right;
p_left = p_left -> right;
p_right = p_right -> left;
}
return root;
}
};
```
## 思路二
``` C++
public:
Node* connect(Node* root) {
if(root == NULL) return NULL;
Node *start = root, *p = NULL;
while(start){
p = start;
// p从每层开始节点起往右移动
while(p){
if(p -> left) { // 如果不是最后一层
p -> left -> next = p -> right;
if(p -> next) // 如果不是本层的最后一个节点
p -> right -> next = p -> next -> left;
}
p = p -> next;
}
start = start -> left; // 处理下一层
}
return root;
}
};
```