LeetCode 113. Path Sum II
题目描述:
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
For example:
Given the below binary tree and
1 sum = 22,
1
2
3
4
5
6
7
8 5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1return
1
2
3
4 [
[5,4,11,2],
[5,8,4,5]
]
与上一题类似, 只不过在DFS的过程中维护一个路径path, 保存从根节点到当前节点的值, 抵达叶子节点并且path中元素的和与sum相等时就把它加到结果中.
1 | /** |