Balanced Binary Tree——LeetCode
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。
第一种方式:递归算出每个节点的左右子树的高度,如果相差超过1,直接返回false。
第二种方式:DFS遍历一遍,遇到左右子树高度差超过1的,记录一个全局变量isBlance为false,最后返回这个boolean的全局变量即可。
显然,第二种方式更优,只需要从根节点计算一遍即可,而第一种方式有很多重复计算。
Talk is cheap>>
第一种方式:
public class BalancedBinaryTree {
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
List<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.get(0);
queue.remove(0);
if (Math.abs(maxDepth(node.left) - maxDepth(node.right)) > 1)
return false;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return true;
} public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return 1 + Math.max(l, r);
}
}
第二种方式:
private boolean result = true; public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
} public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1)
result = false;
return 1 + Math.max(l, r);
}
Balanced Binary Tree——LeetCode的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Balanced Binary Tree [LeetCode]
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree leetcode java
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Balanced Binary Tree --Leetcode C++
递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- CSS控制LI行字符溢出用省略号取代
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- RJ45口线序的理解
RJ45线序就是TX_P / TX_N / RX_P / RX_N 四根线,分别用到的是1,2,3,6 因为TX要匹配RX,所以 线1 变成 另一端的 线3, 线2 变成 另一端的 线6 反过来也一样
- android - INSTALL_FAILED_MEDIA_UNAVAILABLE
解决方案是将'AndroidManifest.xml'设置 'installLocation'的属性为'auto'即可.
- mssql sql高效关联子查询的update 批量更新
/* 使用带关联子查询的Update更新 --1.创建测试表 create TABLE Table1 ( a varchar(10), b varchar(10), ...
- [转]mysql 的日志的启动与查看
mysql有以下几种日志:错误日志: -log-err查询日志: -log慢查询日志: -log-slow-queries更新日志: -log-update二进制日志:-log-bin 日志 ...
- JSON.parse和JSON.stringify 参数详解
JSON.parse和JSON.stringify这两个浏览器自带(IE6/7除外)的方法平常我们经常用到,但是一般都只是用到了他们的第一个参数,比如字符串转对象:JSON.parse('{}') ...
- JSON带来编程界怎样的描述
JSON是一套数据对象组织格式,从程序员的角度观看,他是以种非常易读易写的形式来描述一种key-value的数据组织.全名称JavaScript Object Notation,从名称上可看已经说明他 ...
- iOS开发,新手入门指导
在做了近两年wp,安卓开发之后,某一天突然决定投身iOS的开发之中. 因为一直用的mac,做wp开发的时候都用双系统,vs开久了,就会比较烫,这点让人不爽.后来更多地做安卓,直接mac下开发,很舒适的 ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- PAT - IO - 螺旋方阵
所谓“螺旋方阵”,是指对任意给定的N,将1到N*N的数字从左上角第1个格子开始,按顺时针螺旋方向顺序填入NxN的方阵里.本题要求构造这样的螺旋方阵. 输入格式: 输入在一行中给出一个正整数N(< ...