129. Sum Root to Leaf Numbers

基本的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     }

 

posted @ 2016-06-16 05:44  warmland  阅读(124)  评论(0编辑  收藏  举报