104. Maximum Depth of Binary Tree,depthbinary
1 public class MaximumDepthofBinaryTree104
2 {
3 public int maxDepth(TreeNode root) {
4 int res=0;
5
6 Queue<TreeNode> q1=new LinkedList<TreeNode>();
7 if(root==null)
8 return 0;
9 q1.offer(root);
10 int size=q1.size();
11 while(!q1.isEmpty())
12 {
13 TreeNode temp=q1.poll();
14 size--;
15 if(temp.left!=null)
16 q1.offer(temp.left);
17 if(temp.right!=null)
18 q1.offer(temp.right);
19 if(size==0)
20 {
21 res+=1;
22 size=q1.size();
23 }
24 }
25 return res;
26
27 }
28 }