solution

Your are given the root node r of a binary tree T. Determine the largest distance from r to a leaf. The distance between a node x and one of its descendants y is the number of edges that are between x and y. For example, the distance from x to itself is 0, the distance from x to its children is 1, and so on.

Starter Code:

import java.io.*;
import java.util.*;
public class Lab5
{
/**
* Problem 1: Determine the number of nodes based on their number of children.
*/
private static int[] problem1(Node root)
{
// Implement me!
return new int[] {
-1, // nodes with 0 children
-1, // nodes with 1 child
-1 // nodes with 2 children
};
}
/**
* Problem 2: Determine the maximum distance from the root to a leaf.
*/
private static int problem2(Node root)
{
// Implement me!
return -1;
}
// ———————————————————————
// Do not change any of the code below!
static class Node
{
public int value;
public Node left;
public Node right;
}
private static final int LabNo = 5;
private static final String classNum = “CS 301”;
private static final Random rng = new Random(654321);
public static void main(String args[])
{
System.out.println(classNum + ” — Lab ” + LabNo);
testProblems(1);
testProblems(2);
}
private static boolean testProblem1(int[][] testCase)
{
int[] left = testCase[0];
int[] right = testCase[1];
int[] solution = testCase[2];
Node root = makeTree(left, right);

int[] answer = problem1(root);
if (answer == null || answer.length != solution.length) return false;
for (int i = 0; i < answer.length; i++)
{
if (answer[i] != solution[i]) return false;
}
return true;
}
private static boolean testProblem2(int[][] testCase)
{
int[] left = testCase[0];
int[] right = testCase[1];
int solution = testCase[2][0];
Node root = makeTree(left, right);
int answer = problem2(root);
return solution == answer;
}
private static void testProblems(int prob)
{
int noOfLines = 10000;
System.out.println(“– — — — –“);
System.out.println(noOfLines + ” test cases for problem ” + prob + “.”);
boolean passedAll = true;
for (int i = 1; i <= noOfLines; i++)
{
boolean passed = false;
boolean exce = false;
int[][] testCase = null;
try
{
switch (prob)
{
case 1:
testCase = createProblem1(i);
passed = testProblem1(testCase);
break;
case 2:
testCase = createProblem2(i);
passed = testProblem2(testCase);
break;
}
}
catch (Exception ex)
{

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Looking for a Similar Assignment? Our Experts can help. Use the coupon code SAVE30 to get your first order at 30% off!

Hi there! Click one of our representatives below and we will get back to you as soon as possible.

Chat with us on WhatsApp