剑指Offer_39_平衡二叉树
来源:互联网 发布:directx9修复软件64位 编辑:程序博客网 时间:2024/06/09 16:58
题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
解题思路
递归判断子树是否是平衡二叉树,每次还需要返回子树的高度用以判断当前树是否是平衡的。
实现
public class Solution { private class Result{ boolean isBalanced = false; int depth = 0; public Result(boolean isBalanced, int depth){ this.isBalanced = isBalanced; this.depth = depth; } } public boolean IsBalanced_Solution(TreeNode root) { Result result = isBalanced(root); return result.isBalanced; } private Result isBalanced(TreeNode root) { if (root == null) return new Result(true, 0); Result left = isBalanced(root.left); Result right = isBalanced(root.right); int depth = left.depth > right.depth ? left.depth + 1 : right.depth + 1; if (!left.isBalanced || !right.isBalanced){ return new Result(false,depth); } if (left.depth - right.depth > 1 || right.depth - left.depth > 1){ return new Result(false,depth); }else { return new Result(true,depth); } }}
0 0
- 剑指Offer_39_平衡二叉树
- 剑指offer--平衡二叉树
- 《剑指offer》平衡二叉树
- 剑指Offer--平衡二叉树
- 剑指offer-平衡二叉树
- 剑指offer:平衡二叉树
- 剑指offer 平衡二叉树
- 剑指offer:平衡二叉树
- [剑指offer]平衡二叉树
- 剑指offer|平衡二叉树
- 《剑指offer》-平衡二叉树
- 剑指offer-平衡二叉树
- 剑指offer: 平衡二叉树
- 剑指offer:平衡二叉树
- 剑指offer-平衡二叉树
- 剑指offer 平衡二叉树
- 【剑指offer】平衡二叉树
- 剑指offer-平衡二叉树
- 【codeforces 707A Brain's Photos】
- android自定义控件(超详细)
- fresco的详细了解
- nyoj 不是匹配(思维)
- [USACO Training] Section 2.2
- 剑指Offer_39_平衡二叉树
- python利用pip安装包注意事项
- 虚拟内存与物理内存的区别
- iOS安全—阻止tweak注入hook api
- 为ListView设计数据为空的View
- POJ 3090 Visible Lattice Points
- Linux下安装jdk
- Python与Java之间Socket通信
- Google酝酿中名叫Fuchsia的神秘新系统