Description
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22
,
1 | 5 |
Return:
1 | [ |
解法
与之前的题目类似,不过需要记录路径,同样采用递归的方法解题,注意list中元素为整型时的删除操作,这里采用index的方式删除,如果对应元素删除的话需转换成Integer类型。
具体代码如下:
1 | class Solution { |