[Leetcode]--Path Sum

Recursion Method

[Leetcode]--Path Sum
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null){
            return false;
        }
        
        if(root.left == null && root.right == null && root.val == sum){
            return true;
        } else {
            
            return hasPathSum(root.left, sum - root.val) ||
                   hasPathSum(root.right, sum - root.val);
        }
    }
}
[Leetcode]--Path Sum

[Leetcode]--Path Sum

上一篇:好的算法和差的算法对同一需求的实现,性能相差很大。


下一篇:C# XML创建和解析