Fill in the method template below to make a working method. Make sure your metho
ID: 3772569 • Letter: F
Question
Fill in the method template below to make a working method. Make sure your method conforms to the instructions given in the comments for the method.
public static nodeSum( ){
/* This recursive method takes a TreeNode root as a parameter and returns an integer, the sum of node values in a binary tree. FULL CREDIT WILL ONLY BE AWARDED FOR A RECURSIVE SOLUTION. You may use the following TreeNode operations: node.getLeftChild() – returns the left child of the TreeNode node node.getRightChild() – returns the right child of the TreeNode node node.getData() – returns the integer stored in the TreeNode node
Explanation / Answer
public static int nodeSum(struct *treenode )
{
node *left,*right;
left=treenode.getLeftChild();
right=treenode.getRightChild();
return(treenode->info+nodesum(left)+nodesum(right));
}
In this method initailly treenode is the address of the root node passed to the function.
Every recursive call returns the sum of the parent and sum of left and right nodes.
For any furthur refinements please contact me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.