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 sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
思路:这是一道DFS题,当到达叶子节点时,判断从根节点到叶子节点的和是否等于sum,是的话就将结果存入,不是则回退到父节点的右节点。
class Solution {
pr......
阅读全文