LeetCode 129. Sum Root to Leaf Numbers
2016-03-02 20:53
330 查看
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sum; void process(TreeNode* root, int& sum, int cur){ cur = cur * 10 + root->val; if(!root->left && !root->right){ sum += cur; return; } if(root->left) process(root->left, sum, cur); if(root->right) process(root->right, sum, cur); } int sumNumbers(TreeNode* root) { if(!root) return 0; sum = 0; process(root, sum, 0); return sum; } };
相关文章推荐
- Android学习笔记之ContentProvider
- pku3461 Oulipo 2012-01-11
- 《转载》struts旅程《1》
- Manthan, Codefest 16 -B. A Trivial Problem
- Chrome最新版下载地址
- [LeetCode]101. Symmetric Tree
- MB466光驱位改下SSD硬盘
- SQL笔记(一)
- 交换机的基本命令
- 通过修改短信数据库方法来修改短信 (Ubuntu 环境)
- ORACLE SPATIAL介绍
- 哈希感想
- spark优化
- android:EditText控件imeOptions属性
- poj2478 Farey Sequence 欧拉函数性质的简单应用
- 如何搭建struts2框架
- WinCE项目应用之虚拟仪器(VI)
- XSS安全性过滤
- 【转】C++ 值传递、指针传递、引用传递详解
- bzoj 2313 分组 2012-01-15