码迷,mamicode.com
首页 > 其他好文 > 详细

104. Maximum Depth of Binary Tree

时间:2016-04-01 20:41:56      阅读:190      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!