class Solution {
List<List<Integer>> res=new ArrayList<>();
List<Integer> path=new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int target) {
int count=0;
dfs(root,target,count);
return res;
}
public void dfs(TreeNode root, int target,int count){
if(root==null)return;
path.add(root.val);
count+=root.val;
if(count==target&&root.left==null&&root.right==null){
res.add(new LinkedList(path));
}
dfs(root.left,target,count);
dfs(root.right,target,count);
path.remove(path.size()-1);
}
}
打印二叉树中和为目标值的路径
最新推荐文章于 2022-04-19 17:15:58 发布