Sum Root to Leaf Numbers
来源:互联网 发布:金融街国贸区别知乎 编辑:程序博客网 时间:2024/05/16 12:26
Given a binary tree containing digits from 0-9 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.
dfs.int ans=0;void dfs(TreeNode *root, int temp){ temp=10*temp+root->val; if(root->left==NULL && root->right==NULL) ans+=temp; if(root->left != NULL) dfs(root->left, temp); if(root->right != NULL) dfs(root->right, temp);}int sumNumbers(TreeNode *root){ if(root==NULL) return 0; dfs(root,0); return ans;}
0 0
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- 2014年的六级考试即将来临,你们准备好了吗?
- gbdt 资料
- C++标准库之智能指针类auto_ptr
- Oracle数据库dmp文件Dos命令下导入导出
- T264_param_t, T264_t结构体
- Sum Root to Leaf Numbers
- C++指针类的使用
- 虚拟机的几种网络方式详解(桥接、nat、Host-only)
- 分辨率、帧率、宽高比、像素宽高比等视频参数的概念和区别
- 解决Ubuntu和Windows的文件乱码问题
- hdu1757 矩阵快速幂--
- mybatis获取插入数据主键
- Eclipse常用设置
- 高斯混合模型(GMM)