//层序遍历
public int maxDepth(TreeNode root) {
int deep = 0;
if(root == null)
return deep;
Deque<TreeNode> que = new LinkedList<>();
que.offer(root);
while(!que.isEmpty()){
int len = que.size();
while(len > 0){
TreeNode node = que.poll();
if(node.left!= null)
que.offer(node.left);
if(node.right != null)
que.offer(node.right);
len -- ;
}
deep++;
}
return deep;
}
//递归
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right)+1;
}
非常简单的一道入门题目.值得多反思一些写法
就这样