zoukankan      html  css  js  c++  java
  • [LeetCode#129]Sum Root to Leaf Numbers

    The problem:

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

    An example is the root-to-leaf path 1->2->3 which represents the number 123.

    Find the total sum of all root-to-leaf numbers.

    For example,

        1
       / 
      2   3
    

    The root-to-leaf path 1->2 represents the number 12.
    The root-to-leaf path 1->3 represents the number 13.

    Return the sum = 12 + 13 = 25.

    My first analysis:

    Key:

    1. we search on the tree, and record all pathes in an ArrayList as return value. 

    2. we add the number(represented by each ArrayList) to get the final result. 

    possible pitfall: 

    We usually use sum += val to get the total sum.

    However it's not suitable for this case, since we have already use "sub_sum * 10 + ret.get(i).get(j)" for representing current value.

    You must be very carful about this!!!

    My first solution: 

    Note: sub_sum = sub_sum * 10 + ret.get(i).get(j), not  sub_sum + = sub_sum * 10 + ret.get(i).get(j)
    public class Solution {
        public int sumNumbers(TreeNode root) {
            if (root == null) 
                return 0;
            
            ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>> ();
            ArrayList<Integer> ans = new ArrayList<Integer>();
            
            get_path(root, ans, ret);
            
            int sum = 0;
            int sub_sum = 0;
            for (int i = 0; i < ret.size(); i++) {
                for (int j = 0; j < ret.get(i).size(); j++) {
                    sub_sum = sub_sum * 10 + ret.get(i).get(j); //not sub_sum += sub_sum * 10 + ret.get(i).get(j);   
                }
                sum += sub_sum;
                sub_sum = 0;
            }
            return sum;
        }
        
        private void get_path(TreeNode cur_root, ArrayList<Integer> ans, ArrayList<ArrayList<Integer>> ret) {
         
            if (cur_root == null)
                return;
                
            ArrayList<Integer> cur_list = new ArrayList<Integer> (ans);
            cur_list.add(cur_root.val);
        
            if (cur_root.left == null && cur_root.right == null) {
                ret.add(cur_list);
                return;
            }
            
            get_path(cur_root.left, cur_list, ret);
            get_path(cur_root.right, cur_list, ret);
        }
    }

    A more advanced analysis:

    This quesition involves the testing on whether I have really understand the recursion. The idea underlying the short solution is really very elegant! It's different from the tradition problem we have encountered in following ways:
    1. we won't record(reach) the answers at the lowest recursion level. In fact, since we want to compute the overall sum, we should not think of getting the answer at each branches(they are only partial!!!).
    2. we won't just return a simple -1 or true back. This time we return the result from left-sub tree and right-sub tree. 
    3. we make some prceess at the current level, and then pass it to next recursion level, and wait the next recursion level to return the value back. 

    My second solution:

    public class Solution {
        public int sumNumbers(TreeNode root) {
            
            return helper(root, 0);    
        }
        
        private int helper(TreeNode root, int sum) { //the sum is a little misleading!!! take care!
            
            if (root == null)
                return 0; 
            
            if (root.left == null && root.right == null)
                return sum * 10 + root.val;
            
            return helper(root.left, sum * 10 + root.val) + helper(root.right, sum * 10 + root.val);
        }
    }
  • 相关阅读:
    leetcode 48. Rotate Image
    leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点) 、26/80. Remove Duplicates from Sorted ArrayI、II
    leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
    leetcode 58. Length of Last Word
    安卓操作的一些问题解决
    leetcode 378. Kth Smallest Element in a Sorted Matrix
    android studio Gradle Build速度加快方法
    禁用gridview,listview回弹或下拉悬停
    Android Studio找不到FragmentActivity类
    安卓获取ListView、GridView等滚动的距离(高度)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4214686.html
Copyright © 2011-2022 走看看