Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created April 13, 2014 05:45
Show Gist options
  • Save gabhi/10570853 to your computer and use it in GitHub Desktop.
Save gabhi/10570853 to your computer and use it in GitHub Desktop.
Deepest left leaf node in a binary tree
public class DeepestLeftLeafNodeFinder {
Node deepestNode;
int deepestNodeDepth;
public Node findDeepestLeftLeafNode(Node root,boolean isLeftNode,int depth){
if(isLeftNode){
if(depth > deepestNodeDepth){
deepestNode = root;
deepestNodeDepth = depth;
}
}
if(root.getLeft() != null){
findDeepestLeftLeafNode(root.getLeft(),true,depth + 1);
}
if(root.getRight() != null){
findDeepestLeftLeafNode(root.getRight(),false,depth+1);
}
return deepestNode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment