LeetCode 112 Path Sum
===================
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.
需要注意僅有葉子節(jié)點開始的塔淤,才是符合要求的路徑句各。因為僅當(dāng)left和right為null時殿漠,才判斷sum與當(dāng)前值是否相等瘾婿;若僅有一個left || right為null,則繼續(xù)判斷不為null的一側(cè)。
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
} else {
if (root.left == null && root.right == null) {
if (root.val == sum) return true;
else return false;
} else {
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
}
}