Question: What does the following mystery method do? Give a very short description of what the method returns (not how it does it). Assume the Node

What does the following mystery method do? Give a very short description of what the

method returns (not how it does it). Assume the Node is a binary search tree and make sure

this is relevant to your answer.

class Node {

int value;

Node left;

Node right;

static int mystery(Node n) {

if(n==null)

throw new IllegalArgumentException();

if(n.left == null)

return leftmost(n.right);

if(n.left.left == null && n.left.right == null)

return n.value;

return mystery(n.left);

}

// return leftmost descendant of n

static int leftmost(Node n) {

if(n==null)

throw new IllegalArgumentException();

if(n.left == null)

return n.value;

return leftmost(n.left);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!