/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
//层数就是深度
//建立队列
Queue<TreeNode> q = new LinkedList<>();
while(root == null){
return 0;
}
//根节点放入队列
q.add(root);
int maxDepth = 0;
while(!q.isEmpty()){
maxDepth++;
int size = q.size();
for(int i = 0;i < size;i++){
TreeNode cur = q.poll();
if(cur.left != null){
q.add(cur.left);
}
if(cur.right != null){
q.add(cur.right);
}
}
}
return maxDepth;
}
}