首页 > 其他 > 详细

Balanced Binary Tree

时间:2015-08-12 21:30:32      阅读:204      评论:0      收藏:0      [点我收藏+]

Given a binary tree , determine if it is height-balanced

implement method is in java language,iterator through the height rather than the depth

 

 

public int height(TreeNode root){

     if(root!=null)

          return 0;

 

    int lh = height(root.left);

    int rh = height(root.right);

    if(lh!=-1&&rh!=-1&&Math.abs(lh-rh)<2)

         return 1+Math.max(lh,rh);

    else

         return -1;

}

 

public boolean isBalanced(TreeNode root){

      return height(root)!=-1;

}

Balanced Binary Tree

原文:http://www.cnblogs.com/leilei-lily/p/4725448.html

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