# 0113. 路径总和 II ## 题目地址(113. 路径总和 II) <https://leetcode-cn.com/problems/path-sum-ii/> ## 题目描述 ``` <pre class="calibre18">``` 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 说明: 叶子节点是指没有子节点的节点。 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 返回: [ [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)),这里的所有的解法使用通用方法解答。 除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。 我们先来看下通用解法的解题思路,我画了一张图: ![](https://img.kancloud.cn/46/ec/46ec3382e572355e2109c47284e37e4b_1341x1080.jpg) > 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。 通用写法的具体代码见下方代码区。 ## 关键点解析 - 回溯法 - backtrack 解题公式 ## 代码 - 语言支持:JS,C++,Python3 JavaScript Code: ``` <pre class="calibre18">``` <span class="hljs-title">/* * @lc app=leetcode id=113 lang=javascript * * [113] Path Sum II */</span> <span class="hljs-title">/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">backtrack</span>(<span class="hljs-params">root, sum, res, tempList</span>) </span>{ <span class="hljs-keyword">if</span> (root === <span class="hljs-params">null</span>) <span class="hljs-keyword">return</span>; <span class="hljs-keyword">if</span> (root.left === <span class="hljs-params">null</span> && root.right === <span class="hljs-params">null</span> && sum === root.val) <span class="hljs-keyword">return</span> 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(); } <span class="hljs-title">/** * @param {TreeNode} root * @param {number} sum * @return {number[][]} */</span> <span class="hljs-keyword">var</span> pathSum = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">root, sum</span>) </span>{ <span class="hljs-keyword">if</span> (root === <span class="hljs-params">null</span>) <span class="hljs-keyword">return</span> []; <span class="hljs-keyword">const</span> res = []; backtrack(root, sum, res, []); <span class="hljs-keyword">return</span> res; }; ``` ``` C++ Code: ``` <pre class="calibre18">``` <span class="hljs-title">/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */</span> <span class="hljs-keyword">class</span> Solution { <span class="hljs-keyword">public</span>: <span class="hljs-params">vector</span><<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>> pathSum(TreeNode* root, <span class="hljs-keyword">int</span> sum) { <span class="hljs-keyword">auto</span> ret = <span class="hljs-params">vector</span><<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>>(); <span class="hljs-keyword">auto</span> temp = <span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>(); backtrack(root, sum, ret, temp); <span class="hljs-keyword">return</span> ret; } <span class="hljs-keyword">private</span>: <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">backtrack</span><span class="hljs-params">(<span class="hljs-keyword">const</span> TreeNode* root, <span class="hljs-keyword">int</span> sum, <span class="hljs-params">vector</span><<span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>>& ret, <span class="hljs-params">vector</span><<span class="hljs-keyword">int</span>>& tempList)</span> </span>{ <span class="hljs-keyword">if</span> (root == <span class="hljs-params">nullptr</span>) <span class="hljs-keyword">return</span>; tempList.push_back(root->val); <span class="hljs-keyword">if</span> (root->val == sum && root->left == <span class="hljs-params">nullptr</span> && root->right == <span class="hljs-params">nullptr</span>) { ret.push_back(tempList); } <span class="hljs-keyword">else</span> { backtrack(root->left, sum - root->val, ret, tempList); backtrack(root->right, sum - root->val, ret, tempList); } tempList.pop_back(); } }; ``` ``` ``` <pre class="calibre18">``` <span class="hljs-title"># Definition for a binary tree node.</span> <span class="hljs-title"># class TreeNode:</span> <span class="hljs-title"># def __init__(self, x):</span> <span class="hljs-title"># self.val = x</span> <span class="hljs-title"># self.left = None</span> <span class="hljs-title"># self.right = None</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pathSum</span><span class="hljs-params">(self, root: TreeNode, sum: int)</span> -> List[List[int]]:</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> root: <span class="hljs-keyword">return</span> [] result = [] <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">trace_node</span><span class="hljs-params">(pre_list, left_sum, node)</span>:</span> new_list = pre_list.copy() new_list.append(node.val) <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> node.left <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> node.right: <span class="hljs-title"># 这个判断可以和上面的合并,但分开写会快几毫秒,可以省去一些不必要的判断</span> <span class="hljs-keyword">if</span> left_sum == node.val: result.append(new_list) <span class="hljs-keyword">else</span>: <span class="hljs-keyword">if</span> node.left: trace_node(new_list, left_sum-node.val, node.left) <span class="hljs-keyword">if</span> node.right: trace_node(new_list, left_sum-node.val, node.right) trace_node([], sum, root) <span class="hljs-keyword">return</span> result ``` ``` ## 相关题目 - [39.combination-sum](39.combination-sum.html) - [40.combination-sum-ii](40.combination-sum-ii.html) - [46.permutations](46.permutations.html) - [47.permutations-ii](47.permutations-ii.html) - [78.subsets](78.subsets.html) - [90.subsets-ii](90.subsets-ii.html) - [131.palindrome-partitioning](131.palindrome-partitioning.html)