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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 卢龙县| 甘洛县| 澎湖县| 宁都县| 扎鲁特旗| 吴江市| 湘乡市| 左贡县| 黄浦区| 六安市| 建瓯市| 永修县| 安庆市| 盘锦市| 麻阳| 当涂县| 阿拉善左旗| 渑池县| 清河县| 台山市| 南华县| 平泉县| 苍南县| 杭州市| 故城县| 新沂市| 海兴县| 正定县| 南雄市| 济南市| 长兴县| 吐鲁番市| 沙雅县| 新乡县| 蓬莱市| 大渡口区| 鄂托克旗| 商丘市| 五大连池市| 原阳县|