标签:leetcode
https://oj.leetcode.com/problems/path-sum/
http://blog.csdn.net/linhuanmars/article/details/23654413
/** * 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; return check(root, sum, 0); } private boolean check(TreeNode node, int target, int sumSoFar) { sumSoFar += node.val; // A leaf if (node.left == null && node.right == null) { return sumSoFar == target; } boolean r = false; if (node.left != null) r |= check(node.left, target, sumSoFar); if (node.right != null) r |= check(node.right, target, sumSoFar); return r; } }
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1599715