leetcode题目链接 1. 题目考点 bfsjava 栈模拟bfs递归和返回操作 2. 考点解析 bfs + stack List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> pathSum(TreeNode root, int targetSum) { Stack<Integer> path = new Stack<>(); dfs(root, targetSum, path); return res; } public void dfs(TreeNode root, int sum, Stack<Integer> path) { if (root == null) return ; // 关键:模拟递归保存 path.push(root.val); if (root.left == null && root.right == null && root.val == sum) { res.add(new ArrayList(path)); } dfs(root.left, sum-root.val, path); dfs(root.right, sum-root.val, path); // 关键:模拟返回重置 path.pop(); }