跳至主要內容

某个和的所有路径


某个和的所有路径

题目

给定一棵二叉树和一个数字'S',找出从根到叶的所有路径,使每个路径的所有节点值之和等于'S'。

解答

import java.util.*;

class TreeNode {
  int val;
  TreeNode left;
  TreeNode right;

  TreeNode(int x) {
    val = x;
  }
};

class FindAllTreePaths {
  public static List<List<Integer>> findPaths(TreeNode root, int sum) {
    List<List<Integer>> allPaths = new ArrayList<>();
    List<Integer> currentPath = new ArrayList<Integer>();
    findPathsRecursive(root, sum, currentPath, allPaths);
    return allPaths;
  }

  private static void findPathsRecursive(TreeNode currentNode, int sum, List<Integer> currentPath,
      List<List<Integer>> allPaths) {
    if (currentNode == null)
      return;

    // 添加当前节点到路径中
    currentPath.add(currentNode.val);

    // 如果当前节点是叶子结点并且它的值等于当前总和sum,保存当前路径
    if (currentNode.val == sum && currentNode.left == null && currentNode.right == null) {
      allPaths.add(new ArrayList<Integer>(currentPath));
    } else {
      // 遍历左子树
      findPathsRecursive(currentNode.left, sum - currentNode.val, currentPath, allPaths);
      // 遍历右子树
      findPathsRecursive(currentNode.right, sum - currentNode.val, currentPath, allPaths);
    }

    // 从要回溯的路径中删除当前节点 
    // 当我们向上递归调用堆栈时,我们需要删除当前节点。
    currentPath.remove(currentPath.size() - 1);
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(12);
    root.left = new TreeNode(7);
    root.right = new TreeNode(1);
    root.left.left = new TreeNode(4);
    root.right.left = new TreeNode(10);
    root.right.right = new TreeNode(5);
    int sum = 23;
    List<List<Integer>> result = FindAllTreePaths.findPaths(root, sum);
    System.out.println("Tree paths with sum " + sum + ": " + result);
  }
}
上次编辑于:
贡献者: Neil