国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Balanced Binary Tree

2019-11-15 01:07:16
字體:
來源:轉載
供稿:網友
[LeetCode] Balanced Binary Tree

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

For this PRoblem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.

這道題個人覺得不難,搞清楚平衡BTS的定義就好啦。

還有就是boolean method最后不要想當然的想著反正寫了>1就是false就想當然的return true,還是需要用method檢查的哦。

public class Solution {           public boolean isBalanced(TreeNode root) {          if(root == null){              return true;          }          // situation that unbalanced         if(Math.abs(getHeight(root.left)-getHeight(root.right)) > 1){              return false;          }          // recur test balance         return isBalanced(root.left) && isBalanced(root.right);    }        // getHeight method     private int getHeight(TreeNode root){          if(root == null){             return 0;         }          //aviod 0 we can promise root wont be null but root.left and root.right could be null here       return  1+Math.max(getHeight(root.left), getHeight(root.right));      }  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 同江市| 灵川县| 同德县| 大理市| 河北省| 舞钢市| 营山县| 勃利县| 肇州县| 万年县| 洱源县| 临湘市| 莱州市| 大安市| 威宁| 武隆县| 兴仁县| 新沂市| 崇阳县| 西和县| 咸丰县| 开阳县| 乌拉特前旗| 沽源县| 精河县| 巴中市| 砀山县| 林州市| 西丰县| 清水河县| 射阳县| 葫芦岛市| 晋州市| 卢氏县| 宁国市| 海南省| 大同县| 宜章县| 本溪市| 乐安县| 江达县|