Question

In: Computer Science

java. Consider a binary tree with integer values in its nodes, implement a method that returns...

java.

Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.

Solutions

Expert Solution

ANSWER:-

class BinaryTreeSum
{
static class Node
{
int data;
Node left, right;
}
  
/* utility that allocates a new
Node with the given key */
static Node newNode(int key)
{
Node node = new Node();
node.data = key;
node.left = node.right = null;
return (node);
}
  
/* Function to find sum
of all the elements*/
static int Recursive_Sum(Node root) // your required recursive function
{
if (root == null)
return 0;
return (root.data + Recursive_Sum(root.left) +
Recursive_Sum(root.right));
}
  
// Driver Code
public static void main(String args[])
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
  
int sum = Recursive_Sum(root);
System.out.println("Sum of all the elements is: " + sum);
}
}

OUTPUT:-

// If any doubt please comment


Related Solutions

Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
A binary tree model with 7 decision nodes will have how many terminal nodes?
A binary tree model with 7 decision nodes will have how many terminal nodes?
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Give a proof by induction that the number of nodes of a full binary tree with...
Give a proof by induction that the number of nodes of a full binary tree with a height of "h" is: (2^(h+1))-1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT