剑指offer — 平衡二叉树
来源:互联网 发布:u盘中毒数据恢复 编辑:程序博客网 时间:2024/06/05 09:59
输入一棵二叉树,判断该二叉树是否是平衡二叉树
java
class Tree { boolean isBalance; int deep; public Tree(boolean isBalance, int deep) { this.isBalance = isBalance; this.deep = deep; }}public class Solution { public boolean IsBalanced_Solution(TreeNode root) { if (root == null) { return true; } if (root.left == null && root.right == null) { return true; } return isBal(root).isBalance; } private Tree isBal(TreeNode root) { if (root == null) { return new Tree(true, 0); } if (root.left == null && root.right == null) { return new Tree(true, 1); } Tree left = isBal(root.left); Tree right = isBal(root.right); if (!left.isBalance || !right.isBalance) { return new Tree(false, -1); } if (Math.abs(left.deep - right.deep) > 1) { return new Tree(false, -1); } return new Tree(true, Math.max(left.deep, right.deep) + 1); }}
阅读全文
0 0
- 《剑指offer》——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer—平衡二叉树
- 剑指offer — 平衡二叉树
- 剑指offer--平衡二叉树
- 《剑指offer》平衡二叉树
- 剑指Offer--平衡二叉树
- 剑指offer-平衡二叉树
- 剑指offer:平衡二叉树
- 剑指offer 平衡二叉树
- 剑指offer:平衡二叉树
- [剑指offer]平衡二叉树
- 剑指offer|平衡二叉树
- 《剑指offer》-平衡二叉树
- Linux——Samba
- form表单提交中文乱码的详细解析
- 哈尔滨理工大学第七届程序设计竞赛决赛(网络赛-高年级组)- (A,B)
- mybatis(4):增删改查(insert+select+update+delete)(上集)
- PAT乙级1015. 德才论 (25)
- 剑指offer — 平衡二叉树
- IO流(8)--对数据进行加密
- 存储系统科普——单机引擎介绍
- HBitmap Bitmap和CBitmap区别
- mybatis 报错org.apache.ibatis.binding.BindingException
- obj.offsetLeft和obj.style.left区别
- 处理eclipse导入svn后的错误: 找不到或无法加载主类
- ThreadLocal详解 (含源码)
- MySQL SQL命令及常用语法