112 Path Sum

Question:

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

Lesson Learned:

  • Path题可以把path的sum在变量里变化,这样,当path发生变化的时候,sum随之发生变化。如果使用外部变量,则外部变量不在recursion中,需要人工手动加减,非常不方便。

  • return值:如果是return一个path上的值,通常是||, Math.min, Math.max等等,如果是所有path相加,通常是+,-等等

  • ||: 两根遍历有符合值即可 Math.min, Math.max: 两根遍历找最大/最小

  • recursive的变化值可以是传递函数,亦可以是return value。判断是否为真的时候用传递函数变换,用int的时候使用返回值变换。

  • 使用外部变量一般是整个recursion是单向的变化,比如说只是增大或是只是减小,否则就要在recursion里变换很多次。

  • (112)本题的boolean和symmetric的boolean并不一样,symmetric的boolean是每一步都在找boolean,因此用返回参数很好。这道题是寻找有没有这样一个值,因此用外部参数比较好。

  • 因为是path问题,所以用了一个pathSum传递函数来跟踪path的值。

  • 用了path问题的template

Analysis:

First Attempt:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {

    private int total;
    private boolean path;

    public boolean hasPathSum(TreeNode root, int sum) {
        total = 0;
        path = false;
        helper(root, sum);
        return path;
    }

    private void helper (TreeNode root, int sum) {
        if (root == null) {
            return;
        }
        total += root.val;
        if (root.left == null && root.right == null) {
            if (sum == total) {
                path = true;
            }
            total -= root.val;
            return;
        }
        helper(root.left, sum);
        helper(root.right, sum);
        total -= root.val;

    }
}

https://leetcode.com/discuss/10456/accepted-my-recursive-solution-in-java

public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) return false;

        if(root.left == null && root.right == null && sum - root.val == 0) return true;

        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}

updated (12.19.15)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    // (112)本题的boolean和symmetric的boolean并不一样,symmetric的boolean是每一步都在找boolean,因此用返回参数很好。这道题是寻找有没有这样一个值,因此用外部参数比较好。
    // 因为是path问题,所以用了一个pathSum传递函数来跟踪path的值。
    // 用了path问题的template
    private boolean hasSum;
    public boolean hasPathSum(TreeNode root, int sum) {
        hasSum = false;
        if (root == null) {
            return false;
        }
        int pathSum = 0;
        helper(root, sum, pathSum);
        return hasSum;
    }
    private void helper(TreeNode root, int sum, int pathSum) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            if (pathSum + root.val == sum) {
                hasSum = true;
            }
        }
        if (root.left == null) {
            helper(root.right, sum, pathSum + root.val);
        }
        if (root.right == null) {
            helper(root.left, sum, pathSum + root.val);
        }
        helper(root.left, sum, pathSum + root.val);
        helper(root.right, sum, pathSum + root.val);
    }
}