1.题目：

3

/ \ 9 20 / \ 15 7

2.代码：

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
queue.offer(root);
int count=0;
while(!queue.isEmpty()){
int len=queue.size();
for(int i=0; i<len; i++){
TreeNode node = queue.poll();
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
if(i==len-1)  count++;//同一层的最后一个结点时，计数器＋＋
}

}
return count;

}
}``````

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
queue.offer(root);
int count=0;
while(!queue.isEmpty()){
int len=queue.size();
count++;
for(int i=0; i<len; i++){
TreeNode node = queue.poll();
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
//   if(i==len-1)  count++;
}

}
return count;

}
}``````