爱程序网

104. Maximum Depth of Binary Tree

来源: 阅读:

 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 }

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助