Ideas
Similar to the breadth-first algorithm of the graph, the null pointer is the sign of the end of the level traversal
Code
int findHeightOfBinaryTree(BinaryTreeNode root){
int level = 1;
LLQueue q = new LLQueue();
if(root == null)
return 0;
q.enQueue(root);
//作为分层遍历的标志
q.enQueue(null);
while(!q.isEmpty()){
root = q.deQueue();
//当前层遍历结束
if(root == null){
//如果该层非最深层,则为该层增加一个标记
if(!q.isEmpty())
q.enQueue(null);
level++;
}else{
if(root.getLeft() != null){
q.enQueue(root.getLeft());
}
if(root.getRight() != null){
q.enQueue(root.getRight());
}
}
return level;
}
}