Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started