原题目
参考博文
显然是深搜,但是一想到要回退什么的就一团糟,,学习了人家的写法,(easy题,没见过这种类型也不会)
public boolean hasPathSum(TreeNode root,
int sum) {
if(root==
null){
return false;
}
if(root.left==
null&&root.right==
null){
if(root.val==sum){
return true;
}
else{
return false;
}
}
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
}