logo
Interview
    Interview Guide
    Coding Problems List
Sponsored: Coursera
Problems

Binary Tree Path Sum

Problem

Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.

A valid path is from root node to any of the leaf nodes.

Example

Given a binary tree, and target = 5:

     1
    / \
   2   4
  / \
 2   3

return

[
  [1, 2, 2],
  [1, 4]
]

Solution

DFP and keep track of the numbers; if at leaf nodes, check if sum is the same as the target.

Online Judge