1. 题目描述
输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
2. 思路分析
- 每次来到新的节点,记录新节点信息
- 检查新节点是否是叶子节点,如果是,判断路径上的节点值总和是否符合条件;如果不是,继续递归处理左右子树,回到第 1 步
- 最后需要将新节点的信息移除
3. 代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
class Node { constructor(value = 0, left = null, right = null) { this.value = value this.left = left this.right = right } }
function findPath(root, target) { const paths = [] let sum = 0
function _findPath(node, path) { if (node === null) { return }
sum = sum + node.value path.push(node)
const isLeaf = node.left === null && node.right === null
if (isLeaf && sum === target) { paths.push([...path]) }
if (node.left !== null) { _findPath(node.left, path) }
if (node.right !== null) { _findPath(node.right, path) }
sum = sum - node.value path.pop(node) }
_findPath(root, []) return paths }
const root = new Node(1, new Node(2), new Node(3, null, new Node(-1)))
console.log(findPath(root, 3))
|