LeetCode——Balanced Binary Tree
来源:互联网 发布:python exit 1 编辑:程序博客网 时间:2024/06/06 05:45
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的二叉树.
递归判断。
Java:
public boolean isBalanced(TreeNode root) {if (root == null)return true;if (root.left == null && root.right == null)return true;if (Math.abs(depth(root.left) - depth(root.right)) > 1)return false;return isBalanced(root.left) && isBalanced(root.right);}public int depth(TreeNode root) {if (root == null)return 0;return 1 + Math.max(depth(root.left), depth(root.right));}
0 0
- LeetCode——Balanced Binary Tree
- leetcode——Balanced Binary Tree
- Balanced Binary Tree——LeetCode
- LeetCode——Balanced Binary Tree
- leetcode 110 —— Balanced Binary Tree
- LeetCode题解——Balanced Binary Tree
- LeetCode——Balanced Binary Tree
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- Failed to convert property value of type [com.sun.proxy.$Proxy27 implementing com.ibeifeng.se
- HashMap排序
- OpenNI2——初尝试之 环境搭建 与 读取VideoMode
- 设计一个一元稀疏多项式简单的加减法计算器
- Oracle学习(十):视图,索引,序列号,同义词
- LeetCode——Balanced Binary Tree
- OJ2560继承(改错题)
- 跟踪oracle实例的动态注册过程
- makefile笔记
- HDU 1012 u Calculate e(水题)
- Hadoop的整文件读取
- HLG1134
- A标签触发onclick事件而不跳转
- POJ 2388 Who's in the Middle (排序 快排复习)