Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a method to compute the sum of keys at even depth. For example: -If the tree is empty, then the sumAtEvenDepth is 0. -If

Implement a method to compute the sum of keys at even depth. For example:

-If the tree is empty, then the sumAtEvenDepth is 0.

-If the level-order traveral of the tree is 41 21 61 11 31 51, then 41 is at depth 0; 21 and 61 are at depth 1; 11, 31 and 51 are at depth 2. Thus, sumAtEvenDepth is 134, since there are nodes 41,11,31,51 at even depth.

Your method should run in time linear to the size of the tree. You may create a recursive helper function or use a loop. Do not change the class structure or function declaration.

public class BST{

private static class Node {

public Node (int key) {this.key = key;}

private int key;

private Node left;

private Node right;

}

private Node root; //the root of the tree, null for the empty tree

//TODO: implement your method here

Public int sumAtEvenDepth(Node n) {

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions