Java BST find closest leaf

  • Comp Sci
  • Thread starter apiwowar
  • Start date
  • Tags
    Java
  • #1
96
0
Im having trouble with a method that finds the height of the closest leaf. What i have just counts all of the leafs. would i have to separate the recursive calls into two conditional statements to check each one independently? any help or suggestions would be appreciated

this is my method

Code:
//find the distance to the closest leaf 
public int closeLeaf() 
{ 
    int distance;
    return distance = closeLeaf(root);
}

private int closeLeaf(StringNode n)
{
    int dist = 0;

    if(n == null)
    {
        dist = 0;//empty tree
    }
    else if(n.getLeft()== null && n.getRight()== null)
    {
        dist++;
    }

    else
    {

        dist =closeLeaf(n.getLeft()) + closeLeaf(n.getRight());



    }
    return dist;

}
 
  • #2
A breadth first search would hit the nodes top down. You could check if its a leaf as you go top down. Stop it once detected. Never coded a breadth first to help much further than that.
 

Suggested for: Java BST find closest leaf

Replies
7
Views
1K
Replies
17
Views
871
Replies
7
Views
1K
Replies
12
Views
1K
Replies
1
Views
671
Replies
5
Views
2K
Replies
3
Views
793
Replies
2
Views
812
Replies
2
Views
780
Back
Top