LeetCode[Tree]: Sum Root to Leaf Numbers


Given a binary tree containing digits from 0⑼ only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.

这个题目很简单,可用递归做:

class Solution {
public:
int sumNumbers(TreeNode *root) {
sum = 0;
sumNumbers(root, 0);
return sum;
}

private:
int sum;

void sumNumbers(TreeNode *root, int num) {
if (!root) return;

num = num*10 + root->val;
if (root->left == nullptr && root->right == nullptr) sum += num;
if (root->left) sumNumbers(root->left, num);
if (root->right) sumNumbers(root->right, num);
}
};

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » LeetCode[Tree]: Sum Root to Leaf Numbers

150 评论

  1. sildenafil pills robaxin oral order methocarbamol without prescription

  2. order anastrozole 1 mg online cheap buy biaxin online buy sildenafil for sale

  3. doxycycline buy online levitra cheap order lasix 100mg

  4. buy generic furosemide 40mg lasix 100mg us order plaquenil

  5. order generic methotrexate 10mg cheap reglan 20mg order reglan generic

  6. online casino slots no download slots games free purchase cialis sale

  7. buy viagra 50mg without prescription sildenafil 100mg pill order cialis 5mg without prescription

  8. temovate buy online buspar pills cheap cordarone

  9. order famotidine 20mg without prescription buy prograf pills mirtazapine 30mg pills

  10. order cetirizine 5mg pills zyrtec 5mg pill buy zoloft pills for sale

  11. buy atorvastatin 10mg generic oral sildenafil 100mg buy viagra 100mg without prescription

  12. buy clomid pill plaquenil cost buy generic hydroxychloroquine

  13. albenza pills albenza 400 mg cheap order medroxyprogesterone 5mg without prescription

  14. order rosuvastatin 10mg without prescription domperidone buy online order motilium 10mg generic

  15. buy generic betamethasone over the counter betnovate 20 gm cost itraconazole 100mg pills

  16. generic ipratropium 100 mcg linezolid canada buy zyvox 600mg generic

  17. buy nateglinide 120mg captopril price order candesartan 16mg online

  18. clindamycin without prescription fildena usa buy erection pills

  19. order altace 10mg ramipril pills order arcoxia online

  20. buy lopressor 50mg generic atenolol 100mg ca methylprednisolone 8mg tablets

  21. diltiazem over the counter zovirax usa buy generic zyloprim 300mg

  22. purchase clopidogrel generic plavix pill warfarin 5mg canada

  23. where to buy sulfasalazine without a prescription calan for sale calan 240mg for sale

  24. generic irbesartan 300mg buy avapro 150mg order buspar 5mg online cheap

  25. generic glucotrol 5mg nootropil order betnovate 20 gm brand

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡