首页 > 其他 > 详细

104. Maximum Depth of Binary Tree

时间:2016-04-01 20:40:29      阅读:143      评论:0      收藏:0      [点我收藏+]
 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 }

 

104. Maximum Depth of Binary Tree

原文:http://www.cnblogs.com/aguai1992/p/5346132.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!