Question: In java: Write a method named sum that returns the sum of the data values stored in a binary tree. Your method accepts as its

In java:

Write a method named sum that returns the sum of the data values stored in a binary tree. Your method accepts as its parameter a TreeNode that refers to the root of the tree. For example, the following tree has a sum of 33, so if a variable named tree refers to its root, the call of sum(tree) should return 33.

 _____________3 / \ -9____ 15 / \ \ 12 24 0 / \ 3 -9 / \ 0 -6

Assume that you are interacting with TreeNodes as defined below:

public class TreeNode {
 public int data;
 public TreeNode left;
 public TreeNode right;
 
 public TreeNode() { ... }
 public TreeNode(int data) { ... }
 public TreeNode(int data, TreeNode left, TreeNode right) { ... }
}

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!