共计 348 个字符,预计需要花费 1 分钟才能阅读完成。
Leetcode:110. 均衡二叉树
要点:本题的实质还是求二叉树的深度。编写一个函数 depth 用于求子树的深度,得出的左右子树之差的绝对值如果大于 1,那么这棵树不是均衡二叉树,否则该树为均衡二叉树。
class Solution { | |
boolean result = true; | |
public boolean isBalanced(TreeNode root) {depth(root); | |
return result; | |
} | |
public int depth(TreeNode root){if(root == null) return 0; | |
int l = depth(root.left); | |
int r = depth(root.right); | |
if(Math.abs(l - r) > 1) result = false; | |
return Math.max(l , r) + 1; | |
} | |
} |
正文完
发表至: Leetcode个人解题总结
2021-07-03