Given a Binary Tree, compute the sum of all the branches in the tree. 1 branch is the path from the root to a leaf.
We are going to use recursion to solve this problem. But it is important to understand what a branch is.
Consider the binary tree
[1]
/ \
[2] [3]
[1]-[2] = 3[1]-[3] = 4[3, 4]sum array variable, that will keep count on the total sum value that is being calulated.runningSum to the global sums array.sums and initialize it to an empty array.branchSumsHelper(tree, runningSum, sums) helper method that we will use to recurse.left subtree and no right subtree. This means that the node we are currently at is a leaf node.leaf node, push it to the sums array and return from the recursion.branchSumsHelper(tree.left, newRunningSum, sumsArray) and branchSumsHelper(tree.right, newRunningSum, sumsArray)runningSum variable takes care of the current sum that we are adding, hence, we compute the running sum as newRunningSum = runningSum + tree.value;O(n)O(n) - Because the tree can be a skewed tree.