Question
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
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