国产探花免费观看_亚洲丰满少妇自慰呻吟_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));      }  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云和县| 招远市| 同心县| 苍南县| 古田县| 濮阳市| 康马县| 安化县| 万州区| 阿拉善右旗| 玉门市| 高邮市| 无棣县| 勃利县| 东兰县| 阿合奇县| 灵宝市| 南召县| 怀来县| 甘孜县| 葫芦岛市| 合阳县| 水富县| 嫩江县| 景东| 射阳县| 桐城市| 尖扎县| 江陵县| 大石桥市| 乌鲁木齐县| 建湖县| 宜州市| 富民县| 且末县| 辽阳市| 云阳县| 沂南县| 金沙县| 闽侯县| 西畴县|