码迷,mamicode.com
首页 > 其他好文 > 详细

129. Sum Root to Leaf Numbers

时间:2016-06-16 06:51:54      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

基本的dfs

 1     int sum = 0;
 2     
 3     public int sumNumbers(TreeNode root) {
 4         if(root == null) {
 5             return 0;
 6         }
 7         helper(root, 0);
 8         return sum;
 9     }
10     
11     private void helper(TreeNode root, int curSum) {
12         if(root == null) {
13             return;
14         }
15         if(root.left == null && root.right == null) {
16             sum += curSum * 10 + root.val;
17             return;
18         }
19         helper(root.left, curSum * 10 + root.val);
20         helper(root.right, curSum * 10 + root.val);
21     }

 

129. Sum Root to Leaf Numbers

标签:

原文地址:http://www.cnblogs.com/warmland/p/5589655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!