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

Balanced Binary Tree

时间:2015-08-12 21:32:32      阅读:116      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!