diff --git a/README.en.md b/README.en.md index 8897d62ae..43b71edac 100644 --- a/README.en.md +++ b/README.en.md @@ -18,7 +18,10 @@ This repository will be divided into four parts for now: - 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. -- The fourth part is future plans on content that would be introduced into the above parts. +- The fourth part is daily problems which held at group chat. we solve just one problem all together, so that +we can get more feedback, moreover the problems can be drafted to add to the problem solving module. + +- The fifth part is future plans on content that would be introduced into the above parts. > Only when having mastered the basic data structures and algorithms can you solve complex problems easily. @@ -154,6 +157,7 @@ The data structures mainly includes: - [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md) 🆕 - [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md) - [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md) +- [0113.path-sum-ii](./problems/113.path-sum-ii.md) 🆕 - [0131.palindrome-partitioning](./problems/131.palindrome-partitioning.md) 🆕 - [0139.word-break](./problems/139.word-break.md) - [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md) @@ -190,6 +194,7 @@ The data structures mainly includes: - [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md) - [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕 - [0042.trapping-rain-water](./problems/42.trapping-rain-water.md) +- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md) 🆕 - [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md) 🆕 - [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md) - [0146.lru-cache](./problems/146.lru-cache.md) @@ -233,6 +238,12 @@ Latest updated flashcards (only lists the front page): > problems added:#2 #3 #11 +### Daily Problems + +- [summary](./daily/) + +- [project](https://github.com/azl397985856/leetcode/projects/1) + ### Future Plans diff --git a/README.md b/README.md index 632ac7b19..88f49f056 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md) 🆕 - [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md) - [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md) +- [0113.path-sum-ii](./problems/113.path-sum-ii.md) 🆕 - [0131.palindrome-partitioning](./problems/131.palindrome-partitioning.md) 🆕 - [0139.word-break](./problems/139.word-break.md) - [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md) @@ -190,6 +191,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md) - [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕 - [0042.trapping-rain-water](./problems/42.trapping-rain-water.md) +- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md) 🆕 - [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md) 🆕 - [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md) - [0146.lru-cache](./problems/146.lru-cache.md) diff --git a/assets/drawio/124.binary-tree-maximum-path-sum.drawio b/assets/drawio/124.binary-tree-maximum-path-sum.drawio new file mode 100644 index 000000000..142d7f4c0 --- /dev/null +++ b/assets/drawio/124.binary-tree-maximum-path-sum.drawio @@ -0,0 +1 @@ +5Zpdc9o6EIZ/DZd0bH3Y5jIhtL1oOz2T6bS9VG0BmtoWlUUx/fVHxBJYUnJCONSC9iZjrWxZevWw2t14BKdV+0aQ1fI9L2g5AlHRjuDdCIBJnKm/O8O2M6Ak7gwLwYrO1DPcs19UGyNtXbOCNtaNkvNSspVtzHld01xaNiIE39i3zXlpv3VFFtQz3Oek9K2fWSGXnTUD6cH+lrLF0rw5TiZdT0XMzXolzZIUfNMzwdkITgXnsruq2iktd9oZXbrnXj/Ru5+YoLU85oFtI+bFuzb/xH7MpnRxy/758GtstucnKdf2ihu5NRqocZTcqnG7WTJJ71ck3/Vs1IYr21JWpWrF6pI0q24P5qyl6rW3c1aWU15y8TAQnGc5zXNlb6Tg32mv51uGEY5Uj54OFZK2Ty403sunsKO8olJs1S36AYC04ho5rJubw/4Z07K3dcZGNDGL/cAHUdWF1vUlGk88jcG1axxntsYxCCyyeX9PZHjtIsPo0kSOPZHHxlGfReYziIYntmgotGbA02xyYYpBB7M4tGTQd5jnpCzIjzlJ0IWpjPyjH1+9yrGtMohCq4w9ldNrFzl1HEZ4kZPffC4FkRk5MkOMX+HAQqe/9TALE2WlTsAAwsvsZ2XJtcuMI3RxMvuJ2TmT37O4AGyJhuPgohmn9CcdaNjJZi9BZj/VuvrgDDouQLmE4DL72dnVlw28+leWBZf5kYzusjytGwXgSXg2/QTN16wubnZVbtXKS9I0LLeloi2TX9R1pK+/9ux3ba/jbmsatZr7F3PbrvG133N46KFlnuomRguvmO7orybP1yKn/3Uq61BHErGg8tlg1N/RI/dL0JJI9tOe8GObqN/wkTO1lMOvDNvApPEeGDNIt1T93AEGbyi3zpck3lCdGN5QD1ztl/4/UPMDzr8CtfRI1OATzmMY1BBKLT5QlJ2KGjLHmxkKgaFR84PuAVA7IzQmMnseGhASGpg50KQnQwOxPRQG8cDQID/pOBma+OX+KQrkn45HDQZFbeKglp2OWuKgBgdHzU+8BkDtnIfa0fFTFvRQixxoJqdDkzrQoMGh8dPIv8E/mTrl8/4pGoVELXb4OD1+ck9VjAdHzU+lT0btmkL1Y1HrirrBUAMOH/HpqDmnKk6GRg0/Uh8H6NU3VhOxHUtB6bgiLavW1XhF5HLcrCsPRUlbacNn175qXlOnUKZNpGSLekewYoUq++2uvsNyUt7ojooVRflUuUjwdV3sikMPIM55LfW3hcC0e9W3dHaTzM5VfUvskn36yD9HYQJ8AtHL60iqefh+sNv1w0eYcPYv \ No newline at end of file diff --git a/assets/problems/124.binary-tree-maximum-path-sum-1.jpg b/assets/problems/124.binary-tree-maximum-path-sum-1.jpg new file mode 100644 index 000000000..a1875ce87 Binary files /dev/null and b/assets/problems/124.binary-tree-maximum-path-sum-1.jpg differ diff --git a/assets/problems/124.binary-tree-maximum-path-sum.jpg b/assets/problems/124.binary-tree-maximum-path-sum.jpg new file mode 100644 index 000000000..c18e90ca6 Binary files /dev/null and b/assets/problems/124.binary-tree-maximum-path-sum.jpg differ diff --git a/problems/113.path-sum-ii.md b/problems/113.path-sum-ii.md new file mode 100644 index 000000000..cffa360a8 --- /dev/null +++ b/problems/113.path-sum-ii.md @@ -0,0 +1,99 @@ +## 题目地址 +https://leetcode.com/problems/path-sum-ii/description/ + +## 题目描述 +``` +Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. + +Note: A leaf is a node with no children. + +Example: + +Given the below binary tree and sum = 22, + + 5 + / \ + 4 8 + / / \ + 11 13 4 + / \ / \ +7 2 5 1 +Return: + +[ + [5,4,11,2], + [5,8,4,5] +] +``` + +## 思路 + +这道题目是求集合,并不是`求值`,而是枚举所有可能,因此动态规划不是特别切合,因此我们需要考虑别的方法。 + +这种题目其实有一个通用的解法,就是回溯法。 +网上也有大神给出了这种回溯法解题的 +[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。 +除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。 + +我们先来看下通用解法的解题思路,我画了一张图: + +![backtrack](../assets/problems/backtrack.png) + +通用写法的具体代码见下方代码区。 + +## 关键点解析 + +- 回溯法 +- backtrack 解题公式 + + +## 代码 + +```js +/* + * @lc app=leetcode id=113 lang=javascript + * + * [113] Path Sum II + */ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +function backtrack(root, sum, res, tempList) { + if (root === null) return; + if (root.left === null && root.right === null && sum === root.val) + return res.push([...tempList, root.val]); + + tempList.push(root.val); + backtrack(root.left, sum - root.val, res, tempList); + + backtrack(root.right, sum - root.val, res, tempList); + tempList.pop(); +} +/** + * @param {TreeNode} root + * @param {number} sum + * @return {number[][]} + */ +var pathSum = function(root, sum) { + if (root === null) return []; + const res = []; + backtrack(root, sum, res, []); + return res; +}; +``` + +## 相关题目 + +- [39.combination-sum](./39.combination-sum.md) +- [40.combination-sum-ii](./40.combination-sum-ii.md) +- [46.permutations](./46.permutations.md) +- [47.permutations-ii](./47.permutations-ii.md) +- [78.subsets](./78.subsets.md) +- [90.subsets-ii](./90.subsets-ii.md) +- [131.palindrome-partitioning](./131.palindrome-partitioning.md) + + diff --git a/problems/124.binary-tree-maximum-path-sum.md b/problems/124.binary-tree-maximum-path-sum.md new file mode 100644 index 000000000..c9442a9e6 --- /dev/null +++ b/problems/124.binary-tree-maximum-path-sum.md @@ -0,0 +1,120 @@ + +## 题目地址 +https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ + +## 题目描述 + +``` +Given a non-empty binary tree, find the maximum path sum. + +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. + +Example 1: + +Input: [1,2,3] + + 1 + / \ + 2 3 + +Output: 6 +Example 2: + +Input: [-10,9,20,null,null,15,7] + + -10 + / \ + 9 20 + / \ + 15 7 + +Output: 42 +``` + +## 思路 + + 这道题目的path让我误解了,然后浪费了很多时间来解这道题 + 我觉得leetcode给的demo太少了,不足以让我理解path的概念 + 因此我这里自己画了一个图,来补充一下,帮助大家理解path的概念,不要像我一样理解错啦。 + + 首先是官网给的两个例子: + + ![124.binary-tree-maximum-path-sum](../assets/problems/124.binary-tree-maximum-path-sum.jpg) + + 接着是我自己画的一个例子: + + ![124.binary-tree-maximum-path-sum](../assets/problems/124.binary-tree-maximum-path-sum-1.jpg) + +大家可以结合上面的demo来继续理解一下path, 除非你理解了path,否则不要往下看。 + + + 树的题目,基本都是考察递归思想的。因此我们需要思考如何去定义我们的递归函数, + 在这里我定义了一个递归函数,它的功能是,`返回以当前节点为根节点的MathPath` + 但是有两个条件: + + 1. 第一是跟节点必须选择 + 2. 第二是左右子树只能选择一个 + + 为什么要有这两个条件? + + 我的想法是原问题可以转化为: + + 以每一个节点为根节点,我们分别求出max path,最后计算最大值,因此第一个条件需要满足. + + 对于第二个,由于递归函数子节点的返回值会被父节点使用,因此我们如果两个孩子都选择了 + 就不符合max path的定义了,这也是我没有理解题意,绕了很大弯子的原因。 + + + 因此我的做法就是不断调用递归函数,然后在调用过程中不断计算和更新max,最后在主函数中将max返回即可。 + +## 关键点解析 + +- 递归 +- 理解题目中的path定义 + +## 代码 + +```js + + +/* + * @lc app=leetcode id=124 lang=javascript + * + * [124] Binary Tree Maximum Path Sum + */ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +function helper(node, payload) { + if (node === null) return 0; + + const l = helper(node.left, payload); + const r = helper(node.right, payload); + + payload.max = Math.max( + node.val + Math.max(0, l) + Math.max(0, r), + payload.max + ); + + return node.val + Math.max(l, r, 0); +} +/** + * @param {TreeNode} root + * @return {number} + */ +var maxPathSum = function(root) { + if (root === null) return 0; + const payload = { + max: root.val + }; + helper(root, payload); + return payload.max; +}; +``` + +## 相关题目 +- [113.path-sum-ii](./113.path-sum-ii.md) diff --git a/problems/131.palindrome-partitioning.md b/problems/131.palindrome-partitioning.md index 53cf1ec1e..de96e986c 100644 --- a/problems/131.palindrome-partitioning.md +++ b/problems/131.palindrome-partitioning.md @@ -92,4 +92,5 @@ var partition = function(s) { - [47.permutations-ii](./47.permutations-ii.md) - [78.subsets](./78.subsets.md) - [90.subsets-ii](./90.subsets-ii.md) +- [113.path-sum-ii](./113.path-sum-ii.md) diff --git a/problems/39.combination-sum.md b/problems/39.combination-sum.md index dbc5a5cf9..40a07e103 100644 --- a/problems/39.combination-sum.md +++ b/problems/39.combination-sum.md @@ -134,5 +134,6 @@ var combinationSum = function(candidates, target) { - [47.permutations-ii](./47.permutations-ii.md) - [78.subsets](./78.subsets.md) - [90.subsets-ii](./90.subsets-ii.md) +- [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md) diff --git a/problems/40.combination-sum-ii.md b/problems/40.combination-sum-ii.md index 9a73743fe..ab2b7c143 100644 --- a/problems/40.combination-sum-ii.md +++ b/problems/40.combination-sum-ii.md @@ -138,4 +138,5 @@ var combinationSum2 = function(candidates, target) { - [47.permutations-ii](./47.permutations-ii.md) - [78.subsets](./78.subsets.md) - [90.subsets-ii](./90.subsets-ii.md) +- [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md) diff --git a/problems/46.permutations.md b/problems/46.permutations.md index 4a303182c..f00ec1eb0 100644 --- a/problems/46.permutations.md +++ b/problems/46.permutations.md @@ -102,5 +102,6 @@ var permute = function(nums) { - [47.permutations-ii](./47.permutations-ii.md) - [78.subsets](./78.subsets.md) - [90.subsets-ii](./90.subsets-ii.md) +- [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md) diff --git a/problems/47.permutations-ii.md b/problems/47.permutations-ii.md index fe6e8103e..fe230eee0 100644 --- a/problems/47.permutations-ii.md +++ b/problems/47.permutations-ii.md @@ -104,4 +104,5 @@ var permuteUnique = function(nums) { - [46.permutations](./46.permutations.md) - [78.subsets](./78.subsets.md) - [90.subsets-ii](./90.subsets-ii.md) +- [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md) diff --git a/problems/78.subsets.md b/problems/78.subsets.md index d8dc7c277..ce0120dac 100644 --- a/problems/78.subsets.md +++ b/problems/78.subsets.md @@ -112,6 +112,7 @@ var subsets = function(nums) { - [46.permutations](./46.permutations.md) - [47.permutations-ii](./47.permutations-ii.md) - [90.subsets-ii](./90.subsets-ii.md) +- [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md) diff --git a/problems/90.subsets-ii.md b/problems/90.subsets-ii.md index c0a863d92..9b3b58e04 100644 --- a/problems/90.subsets-ii.md +++ b/problems/90.subsets-ii.md @@ -112,6 +112,7 @@ var subsetsWithDup = function(nums) { - [46.permutations](./46.permutations.md) - [47.permutations-ii](./47.permutations-ii.md) - [78.subsets](./78.subsets.md) +- [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md)