找回密码
 立即注册
首页 业界区 安全 剑指offer-24、二叉树中和为某一值的路径(一) ...

剑指offer-24、二叉树中和为某一值的路径(一)

臧莞然 2025-11-5 12:05:47
题⽬描述

输⼊⼀颗⼆叉树的根节点和⼀个整数,按字典序打印出⼆叉树中结点值的和为输⼊整数的所有路径。路径定义为从树的根结点开始往下⼀直到叶结点所经过的结点形成⼀条路径。

  • 该题路径定义为从树的根结点开始往下⼀直到叶⼦结点所经过的结点
  • 叶⼦节点是指没有⼦节点的节点
  • 路径只能从⽗节点到⼦节点,不能从⼦节点到⽗节点
  • 总节点数⽬为 n
例如:给出如下二叉树,sum=22
1.png

返回true ,因为存在⼀条路径 5 -> 4 -> 11 -> 2 的节点值之和为 22
思路及解答

递归回溯法(推荐)

递归回溯法是解决这类问题的经典方法:

  • 前序遍历​:从根节点开始,先访问当前节点,再递归访问左右子节点
  • 路径记录​:使用一个列表记录当前路径上的节点值
  • 目标值递减​:每次递归时将目标值减去当前节点值
  • 叶子节点检查​:到达叶子节点时检查剩余目标值是否为0
  • 回溯处理​:在递归返回前需要移除当前节点,以便尝试其他路径
2.png

3.png
  1. public class Solution {
  2.     // 存储所有符合条件的路径
  3.     List<List<Integer>> result = new ArrayList<>();
  4.     // 存储当前路径
  5.     List<Integer> path = new ArrayList<>();
  6.    
  7.     public List<List<Integer>> FindPath(TreeNode root, int targetSum) {
  8.         if (root == null) {
  9.             return result;
  10.         }
  11.         dfs(root, targetSum);
  12.         return result;
  13.     }
  14.    
  15.     private void dfs(TreeNode node, int remainingSum) {
  16.         if (node == null) {
  17.             return;
  18.         }
  19.         
  20.         // 将当前节点加入路径
  21.         path.add(node.val);
  22.         remainingSum -= node.val;
  23.         
  24.         // 检查是否为叶子节点且路径和等于目标值
  25.         if (node.left == null && node.right == null && remainingSum == 0) {
  26.             result.add(new ArrayList<>(path)); // 必须新建一个ArrayList
  27.         }
  28.         
  29.         // 递归处理左右子树
  30.         dfs(node.left, remainingSum);
  31.         dfs(node.right, remainingSum);
  32.         
  33.         // 回溯,移除当前节点
  34.         path.remove(path.size() - 1);
  35.     }
  36. }
复制代码

  • 时间复杂度:O(n),n 为⼆叉树的节点个数,遍历完所有的节点
  • 空间复杂度:O(n),借助了额外的空间
迭代法(使用栈模拟递归)

使用栈来模拟递归过程,避免递归带来的栈溢出风险:

  • 双栈结构​:一个栈存储节点,一个栈存储剩余目标值
  • 路径记录​:使用链表记录当前路径,方便回溯
  • 后进先出​:按照前序遍历的顺序处理节点
  1. public class Solution {
  2.     public List<List<Integer>> FindPath(TreeNode root, int targetSum) {
  3.         List<List<Integer>> result = new ArrayList<>();
  4.         if (root == null) {
  5.             return result;
  6.         }
  7.         
  8.         Deque<TreeNode> nodeStack = new LinkedList<>();
  9.         Deque<Integer> sumStack = new LinkedList<>();
  10.         Deque<List<Integer>> pathStack = new LinkedList<>();
  11.         
  12.         nodeStack.push(root);
  13.         sumStack.push(targetSum);
  14.         pathStack.push(new ArrayList<>(Arrays.asList(root.val)));
  15.         
  16.         while (!nodeStack.isEmpty()) {
  17.             TreeNode node = nodeStack.pop();
  18.             int remainingSum = sumStack.pop();
  19.             List<Integer> currentPath = pathStack.pop();
  20.             
  21.             // 检查是否为叶子节点且路径和等于目标值
  22.             if (node.left == null && node.right == null && remainingSum == node.val) {
  23.                 result.add(new ArrayList<>(currentPath));
  24.             }
  25.             
  26.             // 右子节点先入栈,保证左子节点先处理
  27.             if (node.right != null) {
  28.                 nodeStack.push(node.right);
  29.                 sumStack.push(remainingSum - node.val);
  30.                 List<Integer> newPath = new ArrayList<>(currentPath);
  31.                 newPath.add(node.right.val);
  32.                 pathStack.push(newPath);
  33.             }
  34.             
  35.             if (node.left != null) {
  36.                 nodeStack.push(node.left);
  37.                 sumStack.push(remainingSum - node.val);
  38.                 List<Integer> newPath = new ArrayList<>(currentPath);
  39.                 newPath.add(node.left.val);
  40.                 pathStack.push(newPath);
  41.             }
  42.         }
  43.         
  44.         return result;
  45.     }
  46. }
复制代码

  • 时间复杂度​:O(n)
  • 空间复杂度​:O(n),需要存储节点和路径信息
BFS层序遍历

使用队列进行广度优先搜索,同时记录路径和:

  • 节点队列​:存储待处理的节点
  • 路径队列​:存储从根节点到当前节点的路径
  • 和队列​:存储从根节点到当前节点的路径和
  1. public class Solution {
  2.     public List<List<Integer>> FindPath(TreeNode root, int targetSum) {
  3.         List<List<Integer>> result = new ArrayList<>();
  4.         if (root == null) {
  5.             return result;
  6.         }
  7.         
  8.         Queue<TreeNode> nodeQueue = new LinkedList<>();
  9.         Queue<List<Integer>> pathQueue = new LinkedList<>();
  10.         Queue<Integer> sumQueue = new LinkedList<>();
  11.         
  12.         nodeQueue.offer(root);
  13.         pathQueue.offer(new ArrayList<>(Arrays.asList(root.val)));
  14.         sumQueue.offer(root.val);
  15.         
  16.         while (!nodeQueue.isEmpty()) {
  17.             TreeNode node = nodeQueue.poll();
  18.             List<Integer> currentPath = pathQueue.poll();
  19.             int currentSum = sumQueue.poll();
  20.             
  21.             // 检查是否为叶子节点且路径和等于目标值
  22.             if (node.left == null && node.right == null && currentSum == targetSum) {
  23.                 result.add(new ArrayList<>(currentPath));
  24.             }
  25.             
  26.             if (node.left != null) {
  27.                 nodeQueue.offer(node.left);
  28.                 List<Integer> newPath = new ArrayList<>(currentPath);
  29.                 newPath.add(node.left.val);
  30.                 pathQueue.offer(newPath);
  31.                 sumQueue.offer(currentSum + node.left.val);
  32.             }
  33.             
  34.             if (node.right != null) {
  35.                 nodeQueue.offer(node.right);
  36.                 List<Integer> newPath = new ArrayList<>(currentPath);
  37.                 newPath.add(node.right.val);
  38.                 pathQueue.offer(newPath);
  39.                 sumQueue.offer(currentSum + node.right.val);
  40.             }
  41.         }
  42.         
  43.         return result;
  44.     }
  45. }
复制代码

  • 时间复杂度​:O(n)
  • 空间复杂度​:O(n),队列存储节点的空间

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册