![](https://img2020.cnblogs.com/blog/2083141/202007/2083141-20200707205914170-1497341814.png)
思路
递归穷举即可
代码
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null){
return false;
}
if(root.left == null && root.right == null){
return sum - root.val == 0;
}
boolean left = false, right = false;
if(root.left != null){
left = hasPathSum(root.left, sum - root.val);
}
if(root.right != null){
right = hasPathSum(root.right, sum - root.val);
}
return left || right;
}
}