{"id":3691,"date":"2018-08-25T08:41:07","date_gmt":"2018-08-25T15:41:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3691"},"modified":"2018-08-25T08:42:44","modified_gmt":"2018-08-25T15:42:44","slug":"leetcode-112-path-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-112-path-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 112. Path Sum"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 112. Path Sum - \u5237\u9898\u627e\u5de5\u4f5cEP22\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/zdClzfnkvDY?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<h1>Problem<\/h1>\n<p>Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.<\/p>\n<p><strong>Note:<\/strong>\u00a0A leaf is a node with no children.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Given the below binary tree and\u00a0<code>sum = 22<\/code>,<\/p>\n<pre class=\"crayon:false\">      <strong>5<\/strong>\r\n     <strong>\/<\/strong> \\\r\n    <strong>4<\/strong>   8\r\n   <strong>\/<\/strong>   \/ \\\r\n  <strong>11<\/strong>  13  4\r\n \/  <strong>\\<\/strong>      \\\r\n7    <strong>2<\/strong>      1\r\n<\/pre>\n<p>return true, as there exist a root-to-leaf path\u00a0<code>5-&gt;4-&gt;11-&gt;2<\/code>\u00a0which sum is 22.<\/p>\n<p>&nbsp;<\/p>\n<h1>Solution: Recursion<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  bool hasPathSum(TreeNode* root, int sum) {\r\n    if (!root) return false;\r\n    if (!root-&gt;left &amp;&amp; !root-&gt;right) return root-&gt;val==sum;\r\n    int new_sum = sum - root-&gt;val;\r\n    return hasPathSum(root-&gt;left, new_sum) || hasPathSum(root-&gt;right, new_sum);\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-113-path-sum-ii\/\">\u82b1\u82b1\u9171 LeetCode 113. Path Sum II<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-437-path-sum-iii\/\">\u82b1\u82b1\u9171 LeetCode 437. Path Sum III<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[222,132,17,28],"class_list":["post-3691","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-path-sum","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3691","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=3691"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3691\/revisions"}],"predecessor-version":[{"id":3694,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3691\/revisions\/3694"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}