[Leetcode 11] 112 Path Sum

Problem:

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.

 

Analysis:

Basically, it's a depth first search problem. We need to enumerate all the possible path sums to check whether there's one path equals to the given sum. We use recursion to solve this problem. If one node's left and right children are all null, we know we reach the end of a path, then we need to test whether this path sum equals to the given value. If equals, we return true immediately. If not, we return false immediately. Then if there's at least one child of the current node is not null, we are in the middle of a path. If only one child, we just consider the sum of this child's path. If two children, we take the "or" result of the two path sums. By using this recursion, we can judge the whether there's one legal path in the given tree.

One thing need to notice is that, the judge sum condition can't be delayed to the node is null, saying "if (node == null) then ...". This judgement is wrong because there is the case that a node has only one child, then the other is null. And given this null, the programm thinks it reaches a path's end while actually this is not the legal path. A simple case is given below:

  1

2

and the sum is 1. The actual result is false while the above method will return true;

 

Code:

View Code
 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public boolean hasPathSum(TreeNode root, int sum) {
12         // Start typing your Java solution below
13         // DO NOT write main() function
14         if (root == null) 
15             return false;
16         else
17             return pathSum(root, 0, sum);
18     }
19     
20     private boolean pathSum(TreeNode node, int pathsum, int sum) {
21         int s = pathsum + node.val;
22         
23         if (node.left == null && node.right == null) {
24             if (s == sum)
25                 return true;
26             else
27                 return false; //do nothing
28         } else
29         if (node.left != null && node.right == null) {
30             return pathSum(node.left, s, sum);
31         } else if (node.left == null && node.right != null) {
32             return (pathSum(node.right, s, sum));
33         } else { //both not null
34             return (pathSum(node.right, s, sum) || pathSum(node.left, s, sum));
35         } 
36     }
37     
38 }

 

Attention:

Learn how to DFS over a tree!

posted on 2013-04-10 12:27  freeneng  阅读(183)  评论(0编辑  收藏  举报

导航