首页 > 其他 > 详细

129. Sum Root to Leaf Numbers

时间:2016-06-16 06:48:59      阅读:252      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!