Java BST find closest leaf

  • Comp Sci
  • Thread starter apiwowar
  • Start date
  • #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;

}
 

Answers and Replies

  • #2
8
0
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.
 

Related Threads on Java BST find closest leaf

Replies
6
Views
3K
Replies
3
Views
982
Replies
3
Views
3K
Replies
6
Views
870
Replies
2
Views
803
  • Last Post
Replies
6
Views
716
  • Last Post
2
Replies
29
Views
2K
  • Last Post
Replies
4
Views
4K
Replies
37
Views
2K
Replies
1
Views
2K
Top