class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> pathSum(TreeNode root, int target) {
if(root == null) return res;
recur(root, target, 0);
return res;
}
public void recur(TreeNode root, int target, int sum){
//截止条件
if(root == null) return;
path.offerLast(root.val);
sum += root.val;
if(root.left == null && root.right == null && sum == target){
res.add(new LinkedList<>(path));
}
recur(root.left, target, sum);
recur(root.right, target, sum);
path.pollLast();
sum -= root.val;
}
}
剑指 Offer 34. 二叉树中和为某一值的路径
最新推荐文章于 2024-05-20 21:56:33 发布