{"id":11456,"date":"2022-05-10T04:10:40","date_gmt":"2022-05-10T04:10:40","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11456"},"modified":"2022-05-10T04:10:40","modified_gmt":"2022-05-10T04:10:40","slug":"programming-problem-sum-root-to-leaf-numbers","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11456","title":{"rendered":"[Programming Problem] Sum Root to Leaf Numbers"},"content":{"rendered":"<pre lang=\"text\">\r\nYou are given the root of a binary tree containing digits from 0 to 9 only.\r\n\r\nEach root-to-leaf path in the tree represents a number.\r\n\r\nFor example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.\r\nReturn the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.\r\n\r\nA leaf node is a node with no children.\r\n\r\nExample 1:\r\nInput: root = [1,2,3]\r\nOutput: 25\r\nExplanation:\r\nThe root-to-leaf path 1->2 represents the number 12.\r\nThe root-to-leaf path 1->3 represents the number 13.\r\nTherefore, sum = 12 + 13 = 25.\r\n\r\n\r\nExample 2:\r\nInput: root = [4,9,0,5,1]\r\nOutput: 1026\r\nExplanation:\r\nThe root-to-leaf path 4->9->5 represents the number 495.\r\nThe root-to-leaf path 4->9->1 represents the number 491.\r\nThe root-to-leaf path 4->0 represents the number 40.\r\nTherefore, sum = 495 + 491 + 40 = 1026.\r\n<\/pre>\n<p><a href=\"https:\/\/leetcode.com\/problems\/sum-root-to-leaf-numbers\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a><\/p>\n<p>Use a recursive algorithm to keep traversing tree, while building the path. Once you reach a leaf node calculate the sum(path + leaf node) and store it in the totalSum.<\/p>\n<pre lang=\"javascript\">\r\n\/**\r\n * Definition for a binary tree node.\r\n * function TreeNode(val, left, right) {\r\n *     this.val = (val===undefined ? 0 : val)\r\n *     this.left = (left===undefined ? null : left)\r\n *     this.right = (right===undefined ? null : right)\r\n * }\r\n *\/\r\n\/**\r\n * @param {TreeNode} root\r\n * @return {number}\r\n *\/\r\nvar sumNumbers = function(root) {\r\n    const sumRef = [0];\r\n    sum(root, [], sumRef);\r\n    return sumRef[0];\r\n};\r\n\r\nvar sum = function(root, path, sumRef) {\r\n    if (root === null) return;\r\n    if (root.left === null && root.right === null) {\r\n        sumRef[0] += Number(path.join('') + root.val);\r\n        return;\r\n    }\r\n\r\n    sum(root.left, [...path, root.val], sumRef);\r\n    sum(root.right, [...path, root.val], sumRef);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11456","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11456"}],"version-history":[{"count":1,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11456\/revisions"}],"predecessor-version":[{"id":11457,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11456\/revisions\/11457"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}