leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法
来源:互联网 发布:淘宝的客服电话是多少 编辑:程序博客网 时间:2024/05/22 06:58
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 of every node never differ by more than 1.
思路:判断是否是平衡二叉树,dfs即可,递归判断每个子树是否平衡二叉树,如果有子树不满足,则整体也不满足。具体代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { /** * 判断是否平衡二叉树 * 看左右子树高度差是否超过1 * 不超过1则分别判断左右子树是否平衡二叉树 */ public boolean isBalanced(TreeNode root) { if(root == null) return true; if(dep(0,root) > - 10) return true; return false; } //计算树的高度 private int dep(int dep,TreeNode root){ if(root == null){ return dep-1; } int dep1 = dep(dep+1,root.left); int dep2 = dep(dep+1,root.right); //如果高度差超过1,返回-10 //则以后的dep返回值均为-10 if(Math.abs(dep1 - dep2) > 1){ return -10; } return dep1 > dep2 ? dep1 : dep2; }}
0 0
- leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法
- Leetcode 110. Balanced Binary Tree 平衡二叉树 解题报告
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- 【LeetCode】Balanced Binary Tree,平衡二叉树
- [LeetCode] Balanced Binary Tree 平衡二叉树
- Leetcode Balanced Binary Tree 平衡二叉树
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- leetcode -- 110. Balanced Binary Tree 【二叉树是否平衡】
- 【平衡二叉树】leetcode 110. Balanced Binary Tree
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
- LeetCode | Balanced Binary Tree(平衡二叉树)
- LeetCode:Balanced Binary Tree(平衡二叉树的判断)
- leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法
- Balanced Binary Tree(平衡二叉树)
- 110. Balanced Binary Tree 平衡二叉树(重点!!)
- 110. Balanced Binary Tree (平衡二叉树判断)
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- Balanced Binary Tree 判断是否平衡二叉树 @LeetCode
- 华为机试题:亮着电灯的盏数
- Json转换利器Gson之实例二-Gson注解和GsonBuilder
- windows主机与虚拟机linux互通
- About Me
- Xcode 文档安装-iOS 8.1 Documentation
- leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法
- android.os.NetworkOnMainThreadException 异常处理
- 黑马程序员_Java基础:JDK1.5后的新特性:自动拆装箱,以及注意事项
- hdu2807The Shortest Path floyd
- 提高开发效率必须遵守的原则
- MySQL数据库管理基本命令
- Python学习之数字
- leetCode 111.Minimum Depth of Binary Tree(二叉树最小深度) 解题思路和方法
- OC习题 -- 切分字符串 处理色值和名称 (知识点: 字典,枚举,数组,字符串)