Sum Root to Leaf Numbers
2013-05-22 09:49
316 查看
Given a binary tree containing digits from
each root-to-leaf path could represent a number.
An example is the root-to-leaf path
represents the number
Find the total sum of all root-to-leaf numbers.
For example,
The root-to-leaf path
the number
The root-to-leaf path
the number
Return the sum = 12 + 13 =
基本题:
0-9only,
each root-to-leaf path could represent a number.
An example is the root-to-leaf path
1->2->3which
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->2represents
the number
12.
The root-to-leaf path
1->3represents
the number
13.
Return the sum = 12 + 13 =
25.
基本题:
int sumNumbers(TreeNode* root) { int total=0; calSum(root,total,0); return total; } void calSum(TreeNode* root,int& tot,int cur) { if (!root) return; cur=cur*10+root->val; if (!root->left&&!root->right) { tot+=cur; return; } if(root->left) calSum(root->left,tot,cur); if(root->right) calSum(root->right,tot,cur); }
相关文章推荐
- Sum Root to Leaf Numbers
- [leetcode]Sum Root to Leaf Numbers
- LeetCode(129) Sum Root to Leaf Numbers
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
- [Leetcode] #129 Sum Root to Leaf Numbers
- [leetcode]Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Java for LeetCode 129 Sum Root to Leaf Numbers
- [LeetCode 129] Sum Root to Leaf Numbers
- LeetCode – Refresh – Sum Root to Leaf Numbers
- leetcode--Sum Root to Leaf Numbers
- LeetCode: Sum Root to Leaf Numbers
- leetcode-sum root to leaf numbers
- 129. Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers - LeetCode 129
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- LeetCode题解——Sum Root to Leaf Numbers
- 【leetcode】【91.6】【M】Sum Root to Leaf Numbers