Skip to content

Commit 021fb89

Browse files
author
luzhipeng
committed
1 parent 39f8250 commit 021fb89

14 files changed

+241
-1
lines changed

Diff for: README.en.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ This repository will be divided into four parts for now:
1818

1919
- The third part is [Anki flashcards](https://apps.ankiweb.net) that record the LeetCode problems in a certain way so as to make it easier to remember.
2020

21-
- The fourth part is future plans on content that would be introduced into the above parts.
21+
- The fourth part is daily problems which held at group chat. we solve just one problem all together, so that
22+
we can get more feedback, moreover the problems can be drafted to add to the problem solving module.
23+
24+
- The fifth part is future plans on content that would be introduced into the above parts.
2225

2326
> Only when having mastered the basic data structures and algorithms can you solve complex problems easily.
2427
@@ -154,6 +157,7 @@ The data structures mainly includes:
154157
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md) 🆕
155158
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
156159
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
160+
- [0113.path-sum-ii](./problems/113.path-sum-ii.md) 🆕
157161
- [0131.palindrome-partitioning](./problems/131.palindrome-partitioning.md) 🆕
158162
- [0139.word-break](./problems/139.word-break.md)
159163
- [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
@@ -190,6 +194,7 @@ The data structures mainly includes:
190194
- [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md)
191195
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
192196
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
197+
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md) 🆕
193198
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md) 🆕
194199
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
195200
- [0146.lru-cache](./problems/146.lru-cache.md)
@@ -233,6 +238,12 @@ Latest updated flashcards (only lists the front page):
233238
234239
> problems added:#2 #3 #11
235240
241+
### Daily Problems
242+
243+
- [summary](./daily/)
244+
245+
- [project](https://github.com/azl397985856/leetcode/projects/1)
246+
236247

237248

238249
### Future Plans

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
154154
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md) 🆕
155155
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
156156
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
157+
- [0113.path-sum-ii](./problems/113.path-sum-ii.md) 🆕
157158
- [0131.palindrome-partitioning](./problems/131.palindrome-partitioning.md) 🆕
158159
- [0139.word-break](./problems/139.word-break.md)
159160
- [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
@@ -190,6 +191,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
190191
- [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md)
191192
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
192193
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
194+
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md) 🆕
193195
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md) 🆕
194196
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
195197
- [0146.lru-cache](./problems/146.lru-cache.md)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-06-30T07:48:36.836Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" etag="0ZosiCEwMByR-dqRotKe" version="10.8.4" type="device"><diagram id="woJiSoYD_9kwfVdstH5T" name="第 1 页">5Z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Xb4OD1+ck9VjAdHzU+lT0btmkL1Y1HrirrBUAMOH/HpqDmnKk6GRg0/Uh8H6NU3VhOxHUtB6bgiLavW1XhF5HLcrCsPRUlbacNn175qXlOnUKZNpGSLekewYoUq++2uvsNyUt7ojooVRflUuUjwdV3sikMPIM55LfW3hcC0e9W3dHaTzM5VfUvskn36yD9HYQJ8AtHL60iqefh+sNv1w0eYcPYv</diagram></mxfile>
54.8 KB
Loading

Diff for: assets/problems/124.binary-tree-maximum-path-sum.jpg

37.8 KB
Loading

Diff for: problems/113.path-sum-ii.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## 题目地址
2+
https://leetcode.com/problems/path-sum-ii/description/
3+
4+
## 题目描述
5+
```
6+
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
7+
8+
Note: A leaf is a node with no children.
9+
10+
Example:
11+
12+
Given the below binary tree and sum = 22,
13+
14+
5
15+
/ \
16+
4 8
17+
/ / \
18+
11 13 4
19+
/ \ / \
20+
7 2 5 1
21+
Return:
22+
23+
[
24+
[5,4,11,2],
25+
[5,8,4,5]
26+
]
27+
```
28+
29+
## 思路
30+
31+
这道题目是求集合,并不是`求值`,而是枚举所有可能,因此动态规划不是特别切合,因此我们需要考虑别的方法。
32+
33+
这种题目其实有一个通用的解法,就是回溯法。
34+
网上也有大神给出了这种回溯法解题的
35+
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
36+
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
37+
38+
我们先来看下通用解法的解题思路,我画了一张图:
39+
40+
![backtrack](../assets/problems/backtrack.png)
41+
42+
通用写法的具体代码见下方代码区。
43+
44+
## 关键点解析
45+
46+
- 回溯法
47+
- backtrack 解题公式
48+
49+
50+
## 代码
51+
52+
```js
53+
/*
54+
* @lc app=leetcode id=113 lang=javascript
55+
*
56+
* [113] Path Sum II
57+
*/
58+
/**
59+
* Definition for a binary tree node.
60+
* function TreeNode(val) {
61+
* this.val = val;
62+
* this.left = this.right = null;
63+
* }
64+
*/
65+
function backtrack(root, sum, res, tempList) {
66+
if (root === null) return;
67+
if (root.left === null && root.right === null && sum === root.val)
68+
return res.push([...tempList, root.val]);
69+
70+
tempList.push(root.val);
71+
backtrack(root.left, sum - root.val, res, tempList);
72+
73+
backtrack(root.right, sum - root.val, res, tempList);
74+
tempList.pop();
75+
}
76+
/**
77+
* @param {TreeNode} root
78+
* @param {number} sum
79+
* @return {number[][]}
80+
*/
81+
var pathSum = function(root, sum) {
82+
if (root === null) return [];
83+
const res = [];
84+
backtrack(root, sum, res, []);
85+
return res;
86+
};
87+
```
88+
89+
## 相关题目
90+
91+
- [39.combination-sum](./39.combination-sum.md)
92+
- [40.combination-sum-ii](./40.combination-sum-ii.md)
93+
- [46.permutations](./46.permutations.md)
94+
- [47.permutations-ii](./47.permutations-ii.md)
95+
- [78.subsets](./78.subsets.md)
96+
- [90.subsets-ii](./90.subsets-ii.md)
97+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
98+
99+

Diff for: problems/124.binary-tree-maximum-path-sum.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/binary-tree-maximum-path-sum/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a non-empty binary tree, find the maximum path sum.
9+
10+
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
11+
12+
Example 1:
13+
14+
Input: [1,2,3]
15+
16+
1
17+
/ \
18+
2 3
19+
20+
Output: 6
21+
Example 2:
22+
23+
Input: [-10,9,20,null,null,15,7]
24+
25+
-10
26+
/ \
27+
9 20
28+
/ \
29+
15 7
30+
31+
Output: 42
32+
```
33+
34+
## 思路
35+
36+
这道题目的path让我误解了,然后浪费了很多时间来解这道题
37+
我觉得leetcode给的demo太少了,不足以让我理解path的概念
38+
因此我这里自己画了一个图,来补充一下,帮助大家理解path的概念,不要像我一样理解错啦。
39+
40+
首先是官网给的两个例子:
41+
42+
![124.binary-tree-maximum-path-sum](../assets/problems/124.binary-tree-maximum-path-sum.jpg)
43+
44+
接着是我自己画的一个例子:
45+
46+
![124.binary-tree-maximum-path-sum](../assets/problems/124.binary-tree-maximum-path-sum-1.jpg)
47+
48+
大家可以结合上面的demo来继续理解一下path, 除非你理解了path,否则不要往下看。
49+
50+
51+
树的题目,基本都是考察递归思想的。因此我们需要思考如何去定义我们的递归函数,
52+
在这里我定义了一个递归函数,它的功能是,`返回以当前节点为根节点的MathPath`
53+
但是有两个条件:
54+
55+
1. 第一是跟节点必须选择
56+
2. 第二是左右子树只能选择一个
57+
58+
为什么要有这两个条件?
59+
60+
我的想法是原问题可以转化为:
61+
62+
以每一个节点为根节点,我们分别求出max path,最后计算最大值,因此第一个条件需要满足.
63+
64+
对于第二个,由于递归函数子节点的返回值会被父节点使用,因此我们如果两个孩子都选择了
65+
就不符合max path的定义了,这也是我没有理解题意,绕了很大弯子的原因。
66+
67+
68+
因此我的做法就是不断调用递归函数,然后在调用过程中不断计算和更新max,最后在主函数中将max返回即可。
69+
70+
## 关键点解析
71+
72+
- 递归
73+
- 理解题目中的path定义
74+
75+
## 代码
76+
77+
```js
78+
79+
80+
/*
81+
* @lc app=leetcode id=124 lang=javascript
82+
*
83+
* [124] Binary Tree Maximum Path Sum
84+
*/
85+
/**
86+
* Definition for a binary tree node.
87+
* function TreeNode(val) {
88+
* this.val = val;
89+
* this.left = this.right = null;
90+
* }
91+
*/
92+
function helper(node, payload) {
93+
if (node === null) return 0;
94+
95+
const l = helper(node.left, payload);
96+
const r = helper(node.right, payload);
97+
98+
payload.max = Math.max(
99+
node.val + Math.max(0, l) + Math.max(0, r),
100+
payload.max
101+
);
102+
103+
return node.val + Math.max(l, r, 0);
104+
}
105+
/**
106+
* @param {TreeNode} root
107+
* @return {number}
108+
*/
109+
var maxPathSum = function(root) {
110+
if (root === null) return 0;
111+
const payload = {
112+
max: root.val
113+
};
114+
helper(root, payload);
115+
return payload.max;
116+
};
117+
```
118+
119+
## 相关题目
120+
- [113.path-sum-ii](./113.path-sum-ii.md)

Diff for: problems/131.palindrome-partitioning.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@ var partition = function(s) {
9292
- [47.permutations-ii](./47.permutations-ii.md)
9393
- [78.subsets](./78.subsets.md)
9494
- [90.subsets-ii](./90.subsets-ii.md)
95+
- [113.path-sum-ii](./113.path-sum-ii.md)
9596

Diff for: problems/39.combination-sum.md

+1
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,6 @@ var combinationSum = function(candidates, target) {
134134
- [47.permutations-ii](./47.permutations-ii.md)
135135
- [78.subsets](./78.subsets.md)
136136
- [90.subsets-ii](./90.subsets-ii.md)
137+
- [113.path-sum-ii](./113.path-sum-ii.md)
137138
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
138139

Diff for: problems/40.combination-sum-ii.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,5 @@ var combinationSum2 = function(candidates, target) {
138138
- [47.permutations-ii](./47.permutations-ii.md)
139139
- [78.subsets](./78.subsets.md)
140140
- [90.subsets-ii](./90.subsets-ii.md)
141+
- [113.path-sum-ii](./113.path-sum-ii.md)
141142
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)

Diff for: problems/46.permutations.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@ var permute = function(nums) {
102102
- [47.permutations-ii](./47.permutations-ii.md)
103103
- [78.subsets](./78.subsets.md)
104104
- [90.subsets-ii](./90.subsets-ii.md)
105+
- [113.path-sum-ii](./113.path-sum-ii.md)
105106
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
106107

Diff for: problems/47.permutations-ii.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@ var permuteUnique = function(nums) {
104104
- [46.permutations](./46.permutations.md)
105105
- [78.subsets](./78.subsets.md)
106106
- [90.subsets-ii](./90.subsets-ii.md)
107+
- [113.path-sum-ii](./113.path-sum-ii.md)
107108
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)

Diff for: problems/78.subsets.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ var subsets = function(nums) {
112112
- [46.permutations](./46.permutations.md)
113113
- [47.permutations-ii](./47.permutations-ii.md)
114114
- [90.subsets-ii](./90.subsets-ii.md)
115+
- [113.path-sum-ii](./113.path-sum-ii.md)
115116
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
116117

117118

Diff for: problems/90.subsets-ii.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ var subsetsWithDup = function(nums) {
112112
- [46.permutations](./46.permutations.md)
113113
- [47.permutations-ii](./47.permutations-ii.md)
114114
- [78.subsets](./78.subsets.md)
115+
- [113.path-sum-ii](./113.path-sum-ii.md)
115116
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
116117

117118

0 commit comments

Comments
 (0)