Deepest node in tree I have written two classes, one to represent the tree and a Util class to get the deepest node from the tree. Can you please review the code?

Below is the class for Tree:

package org.vik.ds.tree;

public class BinaryTree&lt;E&gt;{

private E data;
private BinaryTree&lt;E&gt; leftNode;
private BinaryTree&lt;E&gt; rightNode;

public BinaryTree(E data, BinaryTree&lt;E&gt; leftNode, BinaryTree&lt;E&gt; rightNode)
{
    this.data = data;
    this.leftNode = leftNode;
    this.rightNode = rightNode;
}

public E getData()
{
    return data;
}

public void setData(E data)
{
    this.data = data;
}

public BinaryTree&lt;E&gt; getLeftNode()
{
    return leftNode;
}

public void setLeftNode(BinaryTree&lt;E&gt; leftNode)
{
    this.leftNode = leftNode;
}

public BinaryTree&lt;E&gt; getRightNode()
{
    return rightNode;
}

public void setRightNode(BinaryTree&lt;E&gt; rightNode)
{
    this.rightNode = rightNode;
}

public boolean isLeafNode()
{
    return (leftNode == null &amp;&amp; rightNode == null);

}

}


Below is algo class, which finds deepest node in tree:

public class DeepestNodeInTree{

public static &lt;T&gt; BinaryTree&lt;T&gt; getDeepestNode(BinaryTree&lt;T&gt; rootNode)
{
    BinaryTree&lt;T&gt; deepestNode = rootNode;
    int maxDeeper = 0;

    Deque&lt;BinaryTree&lt;T&gt;&gt; nodeStack = new LinkedList&lt;BinaryTree&lt;T&gt;&gt;();

    BinaryTree&lt;T&gt; tempNode = rootNode;
    Map&lt;BinaryTree&lt;T&gt;, Boolean&gt; isNodeVisitedMap = new HashMap&lt;&gt;();
    while (tempNode != null)
    {

        if (tempNode.isLeafNode())
        {
            int currentMaxDeeperLen = nodeStack.size() - 1;
            if (currentMaxDeeperLen &gt; maxDeeper)
            {
                deepestNode = tempNode;
                maxDeeper = currentMaxDeeperLen;

            }

            isNodeVisitedMap.put(tempNode, true);
            tempNode = nodeStack.isEmpty() ? nodeStack.pop() : null;
        }
        else
        {

            BinaryTree&lt;T&gt; notVisitedChildNode = getNonVisitedChildNode(tempNode, isNodeVisitedMap);
            if (notVisitedChildNode != null)
            {
                nodeStack.push(tempNode);
                tempNode = notVisitedChildNode;
            }
            else
            {
                isNodeVisitedMap.put(tempNode, true);
                tempNode = nodeStack.isEmpty() ? nodeStack.pop() : null;

            }
        }
    }
    return deepestNode;
}

/**
 * This will get left or right node, if both node is visited it will return
 * null
 * 
 * @param tempNode
 * @param isNodeVisitedMap
 * @return
 */
private static &lt;T&gt; BinaryTree&lt;T&gt; getNonVisitedChildNode(BinaryTree&lt;T&gt; tempNode,
        Map&lt;BinaryTree&lt;T&gt;, Boolean&gt; isNodeVisitedMap)
{
    BinaryTree&lt;T&gt; childNode = tempNode.getLeftNode();

    if (isNodeVisited(isNodeVisitedMap, childNode))
    {
        childNode = isNodeVisited(isNodeVisitedMap, tempNode.getRightNode()) ? null : tempNode.getRightNode();
    }
    return childNode;
}

private static &lt;T&gt; boolean isNodeVisited(Map&lt;BinaryTree&lt;T&gt;, Boolean&gt; isNodeVisitedMap, BinaryTree&lt;T&gt; childNode)
{
    return childNode == null || isNodeVisitedMap.containsKey(childNode);
}
}
Your braces do not follow the Standard Java Conventions. The standard way is:

public BinaryTree(E data, BinaryTree&lt;E&gt; leftNode, BinaryTree&lt;E&gt; rightNode) {
    this.data = data;
    this.leftNode = leftNode;
    this.rightNode = rightNode;
}


Notice the movement of the braces.

You should have a constructor for your BinaryTree that takes no parameters and one that takes only the data:

public BinaryTree() {

}

public BinaryTree(E data) {
    this.data = data;
}

public BinaryTree(E data, BinaryTree&lt;E&gt; leftNode, BinaryTree&lt;E&gt; rightNode) {
    this.data = data;
    this.leftNode = leftNode;
    this.rightNode = rightNode;
}


This way, programmers using it has a choice of being able to give less information, and add it later through the set() methods. Also, this allows them to prevent them from doing stuff like:

BinaryTree&lt;Integer&gt; tree = new BinaryTree&lt;Integer&gt;(null, null, null);


After all, who wants to do that?

I don't see any point in making a separate class for finding the deepest node int the tree. Why not just put it under one class?

There is a better way to do it with recursion:

public BinaryTree&lt;E&gt; getDeepestNode(BinaryTree&lt;E&gt; rootNode) {
    if (rootNode.leftNode.isLeafNode() &amp;&amp; rootNode.rightNode.isLeafNode()) {
        return rootNode;
    }
    return getDeepestNode(rootNode.getLeftNode(), 0) &gt; getDeepestNode(
            rootNode.getRightNode(), 0) ? getDeepestNode(rootNode
            .getLeftNode()) : getDeepestNode(rootNode.getRightNode());
}

private int getDeepestNode(BinaryTree&lt;E&gt; rootNode, int depth) {
    if (rootNode.leftNode.isLeafNode() &amp;&amp; rootNode.rightNode.isLeafNode()) {
        return depth + 1;
    }
    return getDeepestNode(rootNode, depth + 1);
}


I know you wanted it without recursion, but I only came up with a solution by using recursion. If you really don't want to use recursion, then your code is fine.