输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
示例:
给定如下二叉树,以及目标和 sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回:
[
[5,4,11,2],
[5,8,4,5]
]
提示:
节点总数 <= 10000
先序遍历+路径记录。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
def dfs(root, sum):
if root is None:
return
path.append(root.val)
if root.val == sum and root.left is None and root.right is None:
res.append(path.copy())
dfs(root.left, sum - root.val)
dfs(root.right, sum - root.val)
path.pop()
if not root:
return []
res = []
path = []
dfs(root, sum)
return res
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<List<Integer>> res;
private List<Integer> path;
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if (root == null) return Collections.emptyList();
res = new ArrayList<>();
path = new ArrayList<>();
dfs(root, sum);
return res;
}
private void dfs(TreeNode root, int sum) {
if (root == null) {
return;
}
path.add(root.val);
if (root.val == sum && root.left == null && root.right == null) {
res.add(new ArrayList<>(path));
}
dfs(root.left, sum - root.val);
dfs(root.right, sum - root.val);
path.remove(path.size() - 1);
}
}
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} sum
* @return {number[][]}
*/
var pathSum = function (root, sum) {
if (!root) return [];
let res = [];
function dfs(node, sum, arr) {
if (!node) return;
arr = [...arr, node.val];
if (node.val === sum && !node.left && !node.right) {
res.push(arr);
return;
}
dfs(node.left, sum - node.val, arr);
dfs(node.right, sum - node.val, arr);
}
dfs(root, sum, []);
return res;
};
var res [][]int
func pathSum(root *TreeNode, sum int) [][]int {
res = [][]int{}
if root == nil {
return res
}
helper(root, sum, []int{})
return res
}
func helper(node *TreeNode, target int, ans []int) {
if node == nil {
return
}
ans = append(ans,node.Val)
target -= node.Val
if target == 0 && node.Left == nil && node.Right == nil {
tmp := make([]int,len(ans))
copy(tmp,ans)
res = append(res,tmp)
} else {
helper(node.Left, target, ans)
helper(node.Right, target, ans)
}
}
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int target) {
vector<vector<int>> ans;
vector<int> path;
dfs(root, ans, path, target);
return ans;
}
void dfs(TreeNode* root, vector<vector<int>>& ans, vector<int>& path, int target) {
if (root == NULL) {
return;
}
target -= root->val;
path.push_back(root->val);
if (root->left == NULL && root->right == NULL) {
if (target == 0) {
ans.push_back(vector<int>(path));
}
}
dfs(root->left, ans, path, target);
dfs(root->right, ans, path, target);
path.pop_back();
}
};