【leetcode】Sum Root to Leaf Numbers
2013-10-05 13:15
561 查看
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sum; void dfs(TreeNode *curNode,int tmpSum) { if(curNode->left==NULL&&curNode->right==NULL) { sum=sum+tmpSum*10+curNode->val; return ; } if(curNode->left!=NULL) dfs(curNode->left,(tmpSum*10+curNode->val)); if(curNode->right!=NULL) dfs(curNode->right,(tmpSum*10+curNode->val)); } int sumNumbers(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(root==NULL) return 0; sum=0; dfs(root,0); return sum; } };
相关文章推荐
- 设计模式之装饰模式
- 例4-6
- DHCP中继
- 1005
- Domino下启用SMTP验证及创建允许匿名验证
- iOS手势
- wifislax4.6最终版reaver穷举PIN教程,新增VM版【8月29日更新】 [
- 禁止搜索引擎收录的方法
- hdu 4091 Zombie’s Treasure Chest 贪心+枚举
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
- poj 2762 Going from u to v or from v to u?(强连通、缩点、拓扑)
- 【ATT】Recover Binary Search Tree
- tomcat内存溢出配置
- ORACLE密码认证方式
- Oracle DB RMAN 简单配置
- Linux Live USB Creator 简体中文版 – 在Windows中用U盘运行Linux
- 封装异常处理的read和write函数
- CF#204DIV2:A. Jeff and Digits
- c语言数组--插入排序
- 数据结构-线性表-顺序存储结构完整可执行代码