• Support PF! Buy your school textbooks, materials and every day products Here!

Help with multiple choice question

  • Thread starter snipez90
  • Start date
1,101
3
1. Homework Statement
Which is true about the method find?

//Return TreeNode with target value, or null if target not found
public static TreeNode find(TreeNode root, Comparable target)
{
if(root == null)
return null;
else if(target.compareTo(root.getValue()) == 0)
return root;
else if(target.compareTo(root.getValue()) < 0)
return find(root.getLeft(), target);
else
return find(root.getRight(), target);
}

A) Method find will never work as intended
B) Method find will always work as intended
C) Method find will only work as intended if target is not in the tree.
D) Method find will always work as intended if the tree is a binary search tree
E) Method find will only work as intended if the tree is a binary search tree and target occurs no more than once in the tree

2. Homework Equations



3. The Attempt at a Solution

I chose E. Since it uses methods getLeft() and getRight(), it appears to work only for binary search trees. Also, it target occured twice, it could return one of two pointers right?
 

Answers and Replies

26
3
I'll agree with you and for the same reasons.
 

Related Threads for: Help with multiple choice question

Replies
6
Views
3K
  • Last Post
Replies
0
Views
828
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
530
Replies
5
Views
644
Top