Root Sum To Leaf Numbers

Tochukwu Munonye
1 min readJan 10, 2021

Given a binary Tree find the sum of root-to-leaf.

A root-to-leaf path is represented as a whole number, for example

[1,2,3] -> root(1) to leaf (2) = 12, and root(1) to leaf(3) = 13.

then sum both numbers = 25.

  1. This problem will involve recursion so first of all, we would create a helper function to calculate the the sum of the two root-to-leaf numbers. Then insert root and 0 into the function.
  2. In the function we’ll check if root is equal to zero, we’ll return zero.
  3. If the root is a leaf, we’ll multiply by sum by 10 and add to the value of the leaf and return it.
  4. Then the last case, if the root is not a leaf, we’ll go down the left of the tree and down the right till we find a leaf and add all of the numbers

--

--