
1
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class BinaryTreePaths {
public List<Integer> findPathToNode(TreeNode root, TreeNode node) {
List<Integer> path = new ArrayList<>();
if (root == null || node == null) return path;
findPath(root, node, new ArrayList<>(), path);
return path;
}
private boolean findPath(TreeNode current, TreeNode target, List<Integer> currentPath, List<Integer> result) {
if (current == null) return false;
currentPath.add(current.val);
if (current == target) {
result.addAll(new ArrayList<>(currentPath));
return true;
}
if (findPath(current.left, target, currentPath, result) ||
findPath(current.right, target, currentPath, result)) {
return true;
}
currentPath.remove(currentPath.size() - 1);
return false;
}
public List<List<Integer>> findPathsThroughNode(TreeNode root, TreeNode node) {
List<List<Integer>> result = new ArrayList<>();
if (root == null || node == null) return result;
List<Integer> pathToNode = findPathToNode(root, node);
if (pathToNode.isEmpty()) return result;
findPathsFromNode(node, new ArrayList<>(), result);
for (List<Integer> path : result) {
path.remove(0);
path.addAll(0, pathToNode);
}
return result;
}
private void findPathsFromNode(TreeNode node, List<Integer> currentPath, List<List<Integer>> result) {
if (node == null) return;
currentPath.add(node.val);
if (node.left == null && node.right == null) {
result.add(new ArrayList<>(currentPath));
} else {
findPathsFromNode(node.left, currentPath, result);
findPathsFromNode(node.right, currentPath, result);
}
currentPath.remove(currentPath.size() - 1);
}
public static void main(String[] args) {
BinaryTreePaths solution = new BinaryTreePaths();
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
TreeNode target = root.left.right;
List<Integer> path1 = solution.findPathToNode(root, target);
System.out.println("条件1路径: " + path1);
List<List<Integer>> paths2 = solution.findPathsThroughNode(root, target);
System.out.println("条件2路径:");
for (List<Integer> path : paths2) {
System.out.println(path);
}
}
}
2
public class ClimbStairs {
public int climbWays(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (i - 2 >= 0) {
dp[i] += dp[i - 2];
}
if (i - 3 >= 0) {
dp[i] += dp[i - 3];
}
}
return dp[n];
}
public int climbWays2(int n) {
if (n < 0) return 0;
if (n == 0) return 1;
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (i >= 1) dp[i] += dp[i - 1];
if (i >= 2) dp[i] += dp[i - 2];
}
return dp[n];
}
public static void main(String[] args) {
ClimbStairs solution = new ClimbStairs();
for (int i = 1; i <= 10; i++) {
System.out.println("到达第" + i + "格的方法数: " + solution.climbWays2(i));
}
}
}
3
import java.util.*;
public class DirectedGraphPaths {
public int countPathsDFS(int[][] edges, int start, int end) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] edge : edges) {
graph.computeIfAbsent(edge[0], k -> new ArrayList<>()).add(edge[1]);
}
return dfs(graph, start, end, new HashSet<>());
}
private int dfs(Map<Integer, List<Integer>> graph, int current, int end, Set<Integer> visited) {
if (current == end) {
return 1;
}
if (!graph.containsKey(current)) {
return 0;
}
int count = 0;
visited.add(current);
for (int neighbor : graph.get(current)) {
if (!visited.contains(neighbor)) {
count += dfs(graph, neighbor, end, visited);
}
}
visited.remove(current);
return count;
}
public int countPathsDP(int[][] edges, int n, int start, int end) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int[] edge : edges) {
graph.get(edge[0]).add(edge[1]);
}
int[] dp = new int[n + 1];
dp[end] = 1;
return memoDFS(graph, start, end, new Integer[n + 1]);
}
private int memoDFS(List<List<Integer>> graph, int current, int end, Integer[] memo) {
if (current == end) {
return 1;
}
if (memo[current] != null) {
return memo[current];
}
int count = 0;
for (int neighbor : graph.get(current)) {
count += memoDFS(graph, neighbor, end, memo);
}
memo[current] = count;
return count;
}
public static void main(String[] args) {
DirectedGraphPaths solution = new DirectedGraphPaths();
int[][] edges = {
{1, 2}, {1, 3}, {2, 3}, {2, 4}, {3, 4}
};
int start = 1;
int end = 4;
int n = 4;
System.out.println("从" + start + "到" + end + "的路径数量(DFS): " +
solution.countPathsDFS(edges, start, end));
System.out.println("从" + start + "到" + end + "的路径数量(DP): " +
solution.countPathsDP(edges, n, start, end));
}
}