Question: public class IntTreeNode { public int data; public IntTreeNode left, right; public IntTreeNode(int d) { data = d; } } public class IntTree { private

public class IntTreeNode { public int data; public IntTreeNode left, right; public IntTreeNode(int d) { data = d; } } public class IntTree { private IntTreeNode overallRoot; public void add(int a) { if (overallRoot == null) overallRoot = new IntTreeNode(a); else add(overallRoot, a); } public void print() { print(overallRoot, 0); } public int depth() { return depth(overallRoot); // launches the recursive method that you select below } private void add(IntTreeNode node, int a) { if (a node.data) { if (node.right == null) node.right = new IntTreeNode(a); else add(node.right, a); } } private void print(IntTreeNode node, int depth) { if (node == null) return; print(node.right, depth+1); for (int i = 0; i -------------------------------------------------------------------------------------------------------------------
a.
private int depth(IntTreeNode node) { return 1 + Math.max(node.left, node.right); } b.
private int depth(IntTreeNode node) { if (node == null) return 1; return Math.max(depth(node.left), depth(node.right)); } c.
private int depth(IntTreeNode node) { if (node == null) return 0; return 1+Math.max(depth(node.left), depth(node.right)); } d.
private int depth(IntTreeNode node) { if (node == null) return 0; return Math.max(node.left.data + 1, node.right.data + 1); } The depth method should print the maximum depth of the tree (depth being the maximum distance from the top of the tree, the root node itself having distance 1, its left and right children having distance 2, their children having distance 3, and so forth). Please select a working depth method for Int Tree. An example main method is provided with the correct value in a comment
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
