{"id":952,"date":"2017-11-27T21:14:24","date_gmt":"2017-11-28T05:14:24","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=952"},"modified":"2018-04-19T08:40:54","modified_gmt":"2018-04-19T15:40:54","slug":"leetcode-113-path-sum-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-113-path-sum-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 113. Path Sum II"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 113. Path Sum II - \u5237\u9898\u627e\u5de5\u4f5cEP23\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/zrN2dxtQ0f0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<div class=\"question-description\">\n<p>Given a binary tree and a sum, find all root-to-leaf paths where each path&#8217;s sum equals the given sum.<\/p>\n<p>For example:<br \/>\nGiven the below binary tree and\u00a0<code>sum = 22<\/code>,<\/p>\n<pre>              5\r\n             \/ \\\r\n            4   8\r\n           \/   \/ \\\r\n          11  13  4\r\n         \/  \\    \/ \\\r\n        7    2  5   1\r\n<\/pre>\n<p>return<\/p>\n<pre>[\r\n   [5,4,11,2],\r\n   [5,8,4,5]\r\n]\r\n<\/pre>\n<\/div>\n<div id=\"interviewed-div\"><strong>Idea:<\/strong><\/div>\n<div>Recursion<\/div>\n<div><\/div>\n<div><strong>Solution:<\/strong><\/div>\n<div>C++<\/div>\n<div><\/div>\n<div>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;vector&lt;int&gt;&gt; pathSum(TreeNode* root, int sum) {\r\n        vector&lt;vector&lt;int&gt;&gt; ans;\r\n        vector&lt;int&gt; curr;\r\n        pathSum(root, sum, curr, ans);\r\n        return ans;\r\n    }\r\nprivate:\r\n    void pathSum(TreeNode* root, int sum, vector&lt;int&gt;&amp; curr, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {\r\n        if(root==nullptr) return;\r\n        if(root-&gt;left==nullptr &amp;&amp; root-&gt;right==nullptr) {\r\n            if(root-&gt;val == sum) {\r\n                ans.push_back(curr);\r\n                ans.back().push_back(root-&gt;val);\r\n            }\r\n            return;\r\n        }\r\n        \r\n        curr.push_back(root-&gt;val);\r\n        int new_sum = sum - root-&gt;val;\r\n        pathSum(root-&gt;left, new_sum, curr, ans);\r\n        pathSum(root-&gt;right, new_sum, curr, ans);\r\n        curr.pop_back();\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-124-binary-tree-maximum-path-sum\/\">LeetCode 124. Binary Tree Maximum Path Sum<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-543-diameter-of-binary-tree\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 543. Diameter of Binary Tree<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-687-longest-univalue-path\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 687. Longest Univalue Path<\/a><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a binary tree and a sum, find all root-to-leaf paths where each path&#8217;s sum equals the given sum. For example: Given the below&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164,45],"tags":[132,17,62],"class_list":["post-952","post","type-post","status-publish","format-standard","hentry","category-medium","category-tree","tag-path-sum","tag-recursion","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=952"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/952\/revisions"}],"predecessor-version":[{"id":2705,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/952\/revisions\/2705"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}