Question: The code below is supposed to calculate and then return a list of all branch sums. so for example a tree thats root is 10

The code below is supposed to calculate and then return a list of all branch sums.

so for example a tree thats root is 10 and the left of the root is 5 and the right of the root is 14, the program should return [15, 24] However it doesnt and I want to know why. I feel like its because of me creating the sums() method and giving it a list return type. Therefore, because of the recursive call, it isnt returning the list like how I expected to, maybe? Please, can someone help explain why? Only method I wrote was sums(). The others are given.

class Program { // This is the class of the input root. Do not edit it. public static class BinaryTree { int value; BinaryTree left; BinaryTree right;

BinaryTree(int value) { this.value = value; this.left = null; this.right = null; } }

public static List branchSums(BinaryTree root) { List list = sums(root, 0); return null; } public static List sums(BinaryTree root, int sum){ if(root == null) return null; int temp = sum + root.value; List list = new ArrayList(); if(root.left == null && root.right == null){ list.add(temp); return list; } else { sums(root.left, temp); sums(root.right, temp); } return list; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!