LeetCode 129. Sum Root to Leaf Numbers
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 129. Sum Root to Leaf Numbers相关的知识,希望对你有一定的参考价值。
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int sum; 13 14 void process(TreeNode* root, int& sum, int cur){ 15 16 cur = cur * 10 + root->val; 17 if(!root->left && !root->right){ 18 sum += cur; 19 return; 20 } 21 if(root->left) process(root->left, sum, cur); 22 if(root->right) process(root->right, sum, cur); 23 24 } 25 26 int sumNumbers(TreeNode* root) { 27 if(!root) return 0; 28 29 sum = 0; 30 process(root, sum, 0); 31 return sum; 32 } 33 };
以上是关于LeetCode 129. Sum Root to Leaf Numbers的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 129. Sum Root to Leaf Numbers
[leetcode-129-Sum Root to Leaf Numbers]
leetcode129 Sum Root to Leaf Numbers
[LeetCode129]Sum Root to Leaf Numbers