<<gs id="715ac1bd-8c0e-41aa-85e5-40c0683b8ad7" ginger_software_uiphraseguid="2c0b6ac1-5158-406d-ba9e-f14a992f0f52" class="GINGER_SOFTWARE_mark">span</gs> style="font-family: Arial, Helvetica, sans-serif; background-color: <gs id="9fb19435-a0d8-45ce-ba05-bb9135c13b5c" ginger_software_uiphraseguid="2c0b6ac1-5158-406d-ba9e-f14a992f0f52" class="GINGER_SOFTWARE_mark">rgb</gs><gs id="5e5d62d2-6faa-491f-bb6d-70ee841729cb" ginger_software_uiphraseguid="2c0b6ac1-5158-406d-ba9e-f14a992f0f52" class="GINGER_SOFTWARE_mark">(</gs>255, 255, 255);">求所有根节点到叶子节点的总和。</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: <gs id="e7b1208f-f30b-4b28-b3f8-7d47c2735f0d" ginger_software_uiphraseguid="2c0b6ac1-5158-406d-ba9e-f14a992f0f52" class="GINGER_SOFTWARE_mark">rgb</gs><gs id="504d1a1c-68e2-4f73-ae7a-eb33e56c4a1b" ginger_software_uiphraseguid="2c0b6ac1-5158-406d-ba9e-f14a992f0f52" class="GINGER_SOFTWARE_mark">(</gs>255, 255, 255);">一个根节点1-2-3 的值定位123。</span>
思路:
针对每一层当前元素*10加上下面一层的值。对于每条路径都要计算。
所以是一个从上到下的过程,往下找一层就记录累加的值num。不需要回溯。
当走到叶子节点跳出。
时间复杂度: O(n) 空间复杂度:O(logn)
Code (C++):
<gs id="6d866855-fe25-46a6-b3b0-b9a7ee8d6057" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">class</gs> Solution <gs id="28bf3ef3-3ff1-4694-bfa1-7896593e1982" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">{
</gs>public:
<gs id="7c0efed6-67ab-46fb-a1d3-23e313f4e10d" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">int</gs> <gs id="64e0295b-0e66-4313-9354-a23f097ae8d9" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">dfs</gs><gs id="3ee02721-0ccd-49f0-970f-960b8234e64a" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">(</gs><gs id="9f49e6a9-c84c-4030-ad22-fd1de5a3c070" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">struct</gs> TreeNode* root, <gs id="767871a6-ca51-4320-91cf-31fb7935f246" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">int</gs> <gs id="96b3c18f-f977-4eaa-bbc6-8e0174d2f274" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">num</gs>) <gs id="2123e43b-62c2-48a5-9c11-1f17506d8a18" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">{
</gs>if (root == NULL) return 0; // if only one child node is NULL, do not count it
if (root->left == NULL && root->right == NULL) return <gs id="6dfd8880-e48a-4079-ac82-84a9cf985501" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">num</gs> * 10 + <gs id="51f1febe-2e2d-4416-82a2-3629b21b9434" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">root</gs>-><gs id="ecb0b869-e589-4575-a8e1-d6ba7fe679e3" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">val</gs>; // if the node is <gs id="d98f57ed-937e-40f5-9dbe-af86bcb28d04" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">leaf node</gs>, return the <gs id="9497f5de-3cb1-4a16-8ee2-e39ebc364a76" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">sum</gs> value
return <gs id="d109be31-7929-4864-a5d0-551b9d6e640e" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">dfs</gs><gs id="c4ff5d42-5b3f-4aba-b792-13e99889df70" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">(</gs>root->left, <gs id="57c7e61a-9dd3-49cb-9e43-639c926467b3" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">num</gs> * 10 + <gs id="e25149bb-ff59-451a-931c-a3e963a5facf" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">root</gs>-><gs id="92d619ea-f06a-474b-8b16-2f9b8f7b1703" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">val</gs>) + <gs id="39e8dc41-5c56-43bb-849d-80f4e2641d3d" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">dfs</gs><gs id="91ac3ea5-449e-4dcc-902a-85ba60720632" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">(</gs>root->right, <gs id="5d68132b-c9a8-4b4d-ba53-79bb0e1abbde" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">num</gs> * 10 + <gs id="1ff7e349-7520-4176-b955-1f5416f4ae47" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">root</gs>-><gs id="21d6ed88-ee4d-4788-a55a-1f4965c8610e" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">val</gs>);<gs id="643cbcc7-fac2-4eda-ab26-071b7eac2e14" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">
}</gs>
<gs id="ad249aed-554d-4d99-b816-f4e4fe0690e9" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">int</gs> <gs id="f58e49ee-6de5-415a-bf11-1269e401e6bf" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">sumNumbers</gs><gs id="71afba17-2005-43a3-90c9-dd1f65314652" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">(</gs><gs id="e98fe588-20fe-4e06-b302-d0683b1d786e" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">struct</gs> TreeNode* root) <gs id="4f6f7825-d924-443e-bb1a-785e3aa9a092" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">{
</gs>return <gs id="ab3c956a-8652-44e7-a4b3-7e98bc4f791f" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">dfs</gs><gs id="8deba8f3-eaa1-4042-b9af-558b584e3a4c" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">(</gs>root, 0);<gs id="f79e7f6b-c0c8-4bc4-a162-e45125e1ba17" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">
}</gs><gs id="1a9f68b6-bee0-4510-93ea-b8ff84f19b81" ginger_software_uiphraseguid="96933f22-19d3-4ff0-ae63-f8b6d214e47c" class="GINGER_SOFTWARE_mark">
}</gs>;
先序遍历。用下层节点来存储。
时间复杂度:O(n) , 空间复杂度:O(叶子节点的个数)
Code (C++):
class Solution {
public:
int sumNumbers(TreeNode* root) {
if(root == NULL) return 0;
int sum;
stack<TreeNode *> stk;
stk.push(root);
while(!stk.empty())
{
TreeNode *cur = stk.top();
stk.pop();
if(cur->left == NULL && cur->right == NULL)
{
sum = sum +cur->val;
}
if(cur->right)
{
cur->right->val = cur->right->val+cur->val * 10; //将下层节点用来存储。这样最后有几个叶子节点就有多少条路径
stk.push(cur->right);
}
if(cur->left)
{
cur->left->val = cur->left->val + cur->val * 10;
stk.push(cur->left);
}
}
return sum;
}
};