sum-root-to-leaf-numbers
2016-06-29 10:35
316 查看
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.
我的代码
**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void sum(TreeNode *root,vector<int>&path,int &res)
{
if(root==NULL)
return;
path.push_back(root->val);
if(root->left==NULL && root->right==NULL)
{
int sum=0;
for(int i=0;i<path.size();i++)
{
sum=sum*10+path[i];
}
res+=sum;
path.pop_back();
}else
{
sum(root->left,path,res);
sum(root->right,path,res);
path.pop_back();
}
}
int sumNumbers(TreeNode *root) {
vector<int>path;
int res=0;
sum(root,path,res);
return res;
}
};
别人的代码
int getsum(TreeNode *root,int num)
{
if(root==NULL)
return 0;
num=num*10+root->val;
if(root->left==NULL &&root->right==NULL)
return num;
return getsum(root->left,num)+getsum(root->right,num);
}
int sumNumbers(TreeNode *root) {
int sum=0;
sum=getsum(root,0);
return sum;
An example is the root-to-leaf path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
1 / \ 2 3
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.
我的代码
**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void sum(TreeNode *root,vector<int>&path,int &res)
{
if(root==NULL)
return;
path.push_back(root->val);
if(root->left==NULL && root->right==NULL)
{
int sum=0;
for(int i=0;i<path.size();i++)
{
sum=sum*10+path[i];
}
res+=sum;
path.pop_back();
}else
{
sum(root->left,path,res);
sum(root->right,path,res);
path.pop_back();
}
}
int sumNumbers(TreeNode *root) {
vector<int>path;
int res=0;
sum(root,path,res);
return res;
}
};
别人的代码
int getsum(TreeNode *root,int num)
{
if(root==NULL)
return 0;
num=num*10+root->val;
if(root->left==NULL &&root->right==NULL)
return num;
return getsum(root->left,num)+getsum(root->right,num);
}
int sumNumbers(TreeNode *root) {
int sum=0;
sum=getsum(root,0);
return sum;
相关文章推荐
- Leaflet的插件在WebGIS中作用
- 德国人的好习惯和好品质(建议永久收藏)
- java基础—注解annotation
- Facebook开源的基于SQL的操作系统检测和监控框架:osquery Table详解
- Java中反射机制详解
- 欢迎使用CSDN-markdown编辑器
- PHP截取用UEditor编辑器保存在数据库的文章乱码和包含HTML标签的问题
- Linux下实现U盘、SD卡自动挂载功能
- OpenResty学习笔记(六) 文件操作
- PHP表单总结
- 1Z0-051-DDL-表的约束
- word-wrap,white-space和text-overflow属性
- leetcode 3---Longest Substring Without Repeating Characters 双指针 String
- 以色列研究人员实现利用计算机风扇噪音窃听
- TS之PCR
- <script>中的type等于text/html时
- windows下面安装Python和pip教程
- 归一化处理的目的和意义
- javascript中数据类型转换那些事
- FreeRTOS 任务管理之任务转移