Question

In: Computer Science

In java, Write a recursive function to calculate the sum of the nodes only on even...

In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function.

private int sumEvenLevels(Node current){ //you can only pass in root.

//code

}

Solutions

Expert Solution

Program: In this program, we calculate the sum of al nodes at the even levels of the tree recursively. Considering root to be at level zero we will be calculating the sum of all even level nodes. Class Node has definition of node, Class Solution has the function sumEvenLevels() and the main() method to test the function.

Below is the implementation:

Code:

class Node {
    int val;
    Node left, right;

    public Node(int val) {
        this.val = val;
        left = right = null;
    }
}

class Solution {

    Node root;

    // Function
    private int sumEvenLevels(Node current) {
        // Base case
        if (current == null) {
            return 0;
        }

        // Declare two child nodes
        Node child = null, child2 = null;

        // Check if current to left is null
        if (current.left != null) {
            // Assign child node to left child
            child = current.left;
        }

        // Check if child2 is null
        if (current.right != null) {

            // Assign child2 to right child
            child2 = current.right;
        }

        // Check if both child and child2 are null
        if (child != null && child2 != null) {

            // Return sum of current val and skip the next level of tree (which is odd)
            return current.val + sumEvenLevels(child.left) + sumEvenLevels(child.right)
                    + sumEvenLevels(child2.left) + sumEvenLevels(child2.right);
        }

        // Check if only child2 is null
        else if (child != null) {
            return current.val + sumEvenLevels(child.left) + sumEvenLevels(child.right);
        }

        // Check if only child is null
        else if (child2 != null) {
            return current.val + sumEvenLevels(child2.left) + sumEvenLevels(child2.right);
        }

        // Else if both are null return current node value
        else {
            return current.val;
        }
    }

    public static void main(String[] args) {

        Solution tree = new Solution();
        tree.root = new Node(5);
        tree.root.left = new Node(3);
        tree.root.right = new Node(7);
        tree.root.left.left = new Node(2);
        tree.root.left.right = new Node(4);
        tree.root.right.left = new Node(6);
        tree.root.right.right = new Node(8);
        tree.root.left.left.left = new Node(13);
        tree.root.left.left.right = new Node(11);
        tree.root.right.right.right = new Node(9);
        tree.root.right.right.left = new Node(7);
        System.out.println("Sum of even level nodes: " + tree.sumEvenLevels(tree.root));

        /*
           5
        /    \
       3      7
      / \    / \
     2   4  6   8
    / \       /  \
   13  11    7    9

         */
    }
}

Output:


Related Solutions

Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
show that every function can be expressed as the sum of an even function and an...
show that every function can be expressed as the sum of an even function and an odd function and that there is only one way to do this.
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
Make a function that calculates the summation of even numbers in the range. The function sum...
Make a function that calculates the summation of even numbers in the range. The function sum takes the two integer parameters and they are used as the range. The function uses default parameters for the range. When we call this function with one argument, it will be used as a starting point and the end of the range will be 100. Also, the function is called without any argument, the default range (0,100) will be used. We will use default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT