LeetCode110. Balanced Binary Tree
来源:互联网 发布:rimworld mac a14 编辑:程序博客网 时间:2024/06/06 01:17
110.Balanced Binary Tree
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.
高度平衡的二叉树定义:二叉树的每个节点的左右子树的高度相差不大于1.
public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
利用递归的思想,先计算树的高度,然后计算每个节点的左右子树的高度差是否大于1.
public boolean isBalanced(TreeNode root) { if(root == null) return true; if((Math.abs(hightOfTree(root.left) - hightOfTree(root.right))) > 1){ return false; } return isBalanced(root.left) && isBalanced(root.right);}public int hightOfTree(TreeNode root){ if(root == null) return 0; return Math.max(hightOfTree(root.left), hightOfTree(root.right)) + 1;}
阅读全文
0 0
- LeetCode110 - Balanced Binary Tree
- LeetCode110-Balanced Binary Tree
- leetcode110---Balanced Binary Tree
- LeetCode110:Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- leetcode110~Balanced Binary Tree
- leetcode110. Balanced Binary Tree
- LeetCode110 Balanced Binary Tree
- leetcode110 Balanced Binary Tree
- Leetcode110. Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- LeetCode110—Balanced Binary Tree
- LeetCode110.Balanced Binary Tree题解
- LeetCode110——Balanced Binary Tree
- LeetCode110 Blanced Binary Tree Java 题解
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- [地图]仿射变换矩阵
- maven创建eclipse工程
- 2017.10.30
- Django Admin管理工具
- centos7 javaweb 链接中文请求tomcat乱码问题解决
- LeetCode110. Balanced Binary Tree
- NLP07-Gensim源码简析[MmCorpus&SvmLightCorpus]
- CSS文件在jsp 文件中应该放的位置
- 简单的顺序表中的一些函数以及一些简单的测试
- 717. 1-bit and 2-bit Characters
- numpy power ValueError: Integers to negative integer powers are not allowed.
- pandas 基本使用
- Dynamic CRM 插件或工作流中调用自定义类库
- 2017.10.28面经