1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java BST find closest leaf

  1. Oct 26, 2011 #1
    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 (Text):
    //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 =closeLeaf(n.getLeft()) + closeLeaf(n.getRight());

        return dist;

  2. jcsd
  3. Oct 26, 2011 #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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Java BST find closest leaf
  1. Java prob. (Replies: 1)

  2. HeapSort Java (Replies: 3)