{"id":3539,"date":"2018-08-16T08:27:09","date_gmt":"2018-08-16T15:27:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3539"},"modified":"2018-08-16T08:27:24","modified_gmt":"2018-08-16T15:27:24","slug":"leetcode-404-sum-of-left-leaves","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-404-sum-of-left-leaves\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 404. Sum of Left Leaves"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 404. Sum of Left Leaves - \u5237\u9898\u627e\u5de5\u4f5c EP5\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/-79mkmH2lZs?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><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(h)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int sumOfLeftLeaves(TreeNode* root) {\r\n    if (!root) return 0;\r\n    if (root-&gt;left &amp;&amp; !root-&gt;left-&gt;left &amp;&amp; !root-&gt;left-&gt;right)\r\n        return root-&gt;left-&gt;val + sumOfLeftLeaves(root-&gt;right);\r\n    return sumOfLeftLeaves(root-&gt;left) + sumOfLeftLeaves(root-&gt;right);\r\n  }\r\n};<\/pre>\n<p>Iterative<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int sumOfLeftLeaves(TreeNode* root) {\r\n    if (!root) return 0;\r\n    queue&lt;TreeNode*&gt; q;\r\n    q.push(root);\r\n    int sum = 0;\r\n    while (!q.empty()) {\r\n      TreeNode* n = q.front();            \r\n      q.pop();\r\n\r\n      TreeNode* l = n-&gt;left;\r\n      if (l) {\r\n        if (!l-&gt;left &amp;&amp; !l-&gt;right)\r\n            sum += l-&gt;val;\r\n        else\r\n            q.push(l);\r\n      }\r\n      if (n-&gt;right) q.push(n-&gt;right);\r\n    }\r\n    return sum;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solution: Recursion Time complexity: O(n) Space complexity: O(h) C++ \/\/ Author: Huahua \/\/ Running time: 4 ms class Solution { public: int sumOfLeftLeaves(TreeNode* root) {&#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":[62,28],"class_list":["post-3539","post","type-post","status-publish","format-standard","hentry","category-tree","tag-sum","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3539","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=3539"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3539\/revisions"}],"predecessor-version":[{"id":3542,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3539\/revisions\/3542"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}