A Binary tree is a non-linear data structure in which each node has maximum of two child nodes. Finding out maximum and minimum values in a binary tree is quite easy. We need not write any special logic to find out maximum or minimum values. Because, the last node to the left of a root contains the minimum value and the last node to the right of a root contains the maximum value.
The java method to find out the minimum value is shown below:
public int getMinimum(tree root)
{
tree p= root;
while(p.left!=null)
p=p.left;
return p.x;
}
Similarly, the java method to find out the maximum value is shown below:
public int getMaximum(tree root)
{
tree p= root;
while(p.right!=null)
p=p.right;
return p.x;
}
1 comments:
what x mean in
return p.x;
???
Post a Comment