class Solution {
int sum;
int tempSum=0;
boolean flag;
List<List<Integer>> list=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null ) return false;
this.sum=sum;
dfs(root);
return flag;
}
public void dfs(TreeNode root){
if(root==null) return ;
if(root.left==null&&root.right==null){
if((tempSum+root.val)==sum ){
flag=true;
}
return ;
}
tempSum+=root.val;
dfs(root.left);
dfs(root.right);
tempSum-=root.val;
}
}
leetcode_112_路径总和@@dfs
猜你喜欢
转载自blog.csdn.net/ruochen82155551/article/details/107622157
今日推荐
周排行