376. Binary Tree Path Sum 和 1353. Sum Root to Leaf Numbers基本一致
Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target . A valid path is from root node to any of the leaf nodes. Have you met this question in a real interview? Yes Problem Correction Example Example 1: Input: {1,2,4,2,3} 5 Output: [[1, 2, 2],[1, 4]] Explanation: The tree is look like this: 1 / \ 2 4 / \ 2 3 For sum = 5 , it is obviously 1 + 2 + 2 = 1 + 4 = 要用就用dfs模板,减小出错的可能。 vector < vector < int >> binaryTreePathSum(TreeNode * root, int target) { // write your code here //典型的DFS vector < vector < int >> res; if (root == NULL ){ return res; } vector < int > path; dfs(root, target, res, path); return res; } void dfs (TreeNode *root, int target, vector < vector < int >> &res, vector < int > &path) { if (root ...