{"id":5738,"date":"2019-10-09T21:11:08","date_gmt":"2019-10-10T04:11:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5738"},"modified":"2019-10-09T21:13:37","modified_gmt":"2019-10-10T04:13:37","slug":"leetcode-129-sum-root-to-leaf-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-129-sum-root-to-leaf-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 129. Sum Root to Leaf Numbers"},"content":{"rendered":"\n<p>Given a binary tree containing digits from&nbsp;<code>0-9<\/code>&nbsp;only, each root-to-leaf path could represent a number.<\/p>\n\n\n\n<p>An example is the root-to-leaf path&nbsp;<code>1-&gt;2-&gt;3<\/code>&nbsp;which represents the number&nbsp;<code>123<\/code>.<\/p>\n\n\n\n<p>Find the total sum of all root-to-leaf numbers.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;A leaf is a node with no children.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,2,3]\n    1\n   \/ \\\n  2   3\n<strong>Output:<\/strong> 25\n<strong>Explanation:<\/strong>\nThe root-to-leaf path <code>1-&gt;2<\/code> represents the number <code>12<\/code>.\nThe root-to-leaf path <code>1-&gt;3<\/code> represents the number <code>13<\/code>.\nTherefore, sum = 12 + 13 = <code>25<\/code>.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [4,9,0,5,1]\n    4\n   \/ \\\n  9   0\n&nbsp;\/ \\\n5   1\n<strong>Output:<\/strong> 1026\n<strong>Explanation:<\/strong>\nThe root-to-leaf path <code>4-&gt;9-&gt;5<\/code> represents the number 495.\nThe root-to-leaf path <code>4-&gt;9-&gt;1<\/code> represents the number 491.\nThe root-to-leaf path <code>4-&gt;0<\/code> represents the number 40.\nTherefore, sum = 495 + 491 + 40 = <code>1026<\/code>.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int sumNumbers(TreeNode* root) {\n    int ans = 0;\n    function<void(TreeNode*, int)> traverse = [&](TreeNode* t, int num) {\n      if (!t) return;\n      num = num * 10 + t->val;\n      if (t->left || t->right) {        \n        traverse(t->left, num);\n        traverse(t->right, num);\n      } else {        \n        ans += num;\n      }\n    };\n    traverse(root, 0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree containing digits from&nbsp;0-9&nbsp;only, each root-to-leaf path could represent a number. An example is the root-to-leaf path&nbsp;1-&gt;2-&gt;3&nbsp;which represents the number&nbsp;123. Find 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":[177,17,28],"class_list":["post-5738","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5738","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=5738"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5738\/revisions"}],"predecessor-version":[{"id":5742,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5738\/revisions\/5742"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}