I tried to answer the following 2 question, they are from a Java course test, but it is a little confusing because of the recursive that I’m probably need to use.
The first one is a method that receive a root of a binary tree and returns the maximum value on tree. (example in figure A).
This question (and the second) says only complete in the missing lines:
public static int maxInTree (Node root)
{
if (root == null)
return 0;
if ((root.getLeftSon() == null) && (root.getRightSon() == null))
______________________ // I think that here: *return 1*;
if (root.getLeftSon() == null)
return _________________
if (___________ == null) // I think that here: *root.getRightSon()*
_______________________________-
return max______________________________
}
The second question says: do the same as the first question BUT for a sorted binary search tree.
public static int maxInSearchTree (Node r)
{
if (r == null)
return 0;
if (r.getRightSon() == null)
__________________________
return _______________________________
}
You can assume there is a method to pullout the father: getNumber().
thnx !!
I assume that getNumber() gives the value (not the father).