#力扣 LeetCode劍指 Offer 55 - II. 平衡二叉樹 @FDDLC
阿新 • • 發佈:2020-12-29
技術標籤:演算法&資料結構
題目描述:
https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/
Java程式碼:
class Solution { public int getHeight(TreeNode root){ return root==null?0:Math.max(getHeight(root.left),getHeight(root.right))+1; } public boolean isBalanced(TreeNode root) { if(root==null)return true; return Math.abs(getHeight(root.left)-getHeight(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right); } }