#include <iostream> #include <vector> #include <unordered_map> #include <string> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) { val = x; left = nullptr; right = nullptr; } }; // 根据前序遍历顺序获取输入 TreeNode* createTree() { TreeNode* root = NULL; string str; cin >> str; if (str != "null") { int num = atoi(str.c_str()); root = new TreeNode(num); root->left = createTree(); root->right = createTree(); return root; } return NULL; } // 递归函数 void recursion(vector<vector<int>> &res, vector<int> &path, TreeNode* root, int target, unordered_map<int, vector<int>> &prefixSum, int curSum) { if (!root) return; curSum += root->val; path.push_back(root->val); int prefixToFind = curSum - target; if (curSum == target) res.push_back(path); if (prefixSum.find(prefixToFind) != prefixSum.end()) { for (int i = 0; i < prefixSum[prefixToFind].size(); i++) { int idx = prefixSum[prefixToFind][i] + 1; res.push_back(vector<int>(path.begin() + idx, path.end())); } } prefixSum[curSum].push_back(path.size() - 1); recursion(res, path, root->left, target, prefixSum, curSum); recursion(res, path, root->right, target, prefixSum, curSum); path.pop_back(); prefixSum[curSum].pop_back(); } // 寻找路径 vector<vector<int>> findPath(TreeNode* root, int target) { vector<vector<int>> res; if (!root) return res; vector<int> path; unordered_map<int, vector<int>> prefixSum; int curSum = 0; recursion(res, path, root, target, prefixSum, curSum); return res; } int main() { TreeNode* root = createTree(); // 根据前序遍历顺序获取输入 [5 4 11 7 8 null null null 2 7 null null 10 null null null 8 13 null null 4 5 6 null null null 1 null null] vector<vector<int>> res = findPath(root, 22); for (int i = 0; i < res.size(); i++) { for (int j = 0; j < res[i].size(); j++) { cout << res[i][j] << " "; // 结果:[5, 4, 11, 7, 8] } cout << endl; } return 0; }
Java:
import java.util.*; public class Solution { public static void main(String[] args) { // String[] nodes = {"5", "4", "8", "11", "null", "13", // "4", "7", "2", "null", "null", "null", "null" // , "5","1"}; // int k = 22; String[] nodes = {"5", "4", "8", "11", "null", "13", "4", "7", "2", "null", "null", "null", "null" , "5","1", "8","null","7", "10", "null", "null", "null","null", "null", "null" ,"null", "null", "6","null", "null", "null"}; int k = 35; TreeNode root = constructTree(nodes, 0, nodes.length); List<List<TreeNode>> list = path(root, k); for (int i = 0; i < list.size(); i++) { for (int j = 0; j < list.get(i).size(); j++) { System.out.print(list.get(i).get(j).val + " "); } System.out.println(); } } public static TreeNode constructTree(String[] nodes, int i, int n) { if(i >= n) return null; String string = nodes[i]; if(string.equals("null")) return null; TreeNode node = new TreeNode(Integer.valueOf(string)); node.left = constructTree(nodes, 2*i+1, n); node.right = constructTree(nodes, 2*i+2, n); return node; } public static List<List<TreeNode>> path(TreeNode root, int sum) { List<List<TreeNode>> list = new ArrayList<>(); List<TreeNode> arr = new ArrayList<>(); if(root == null) return list; Stack<TreeNode> sta = new Stack<>(); sta.add(root); while(!sta.isEmpty()) { root = sta.pop(); findPath(root, sum, list, arr); if(root.right != null) sta.add(root.right); if(root.left != null) sta.add(root.left); } return list; } public static void findPath(TreeNode root, int sum, List<List<TreeNode>> list, List<TreeNode> arr) { sum -= root.val; arr.add(root); if(sum == 0) { list.add(new ArrayList<>(arr)); } if(root.left != null) findPath(root.left, sum, list, arr); if(root.right != null) findPath(root.right, sum, list, arr); arr.remove(arr.size()-1); } } class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(){ } TreeNode(int val){ this.val = val; } }