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 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- QT程序库
实际上,QT不仅仅是一个巨大的程序库,而是7个程序库,还包括许多使用工具,qmake是其中的一个.如今,术语GUI工具包代表的东西的用途不再仅仅是系统提供的那么一个小部分(GUI界面).尤其是QT ...
- Java EE的十三种核心技术
1. JDBC: Java Database Connectivity 2. JNDI: Java Name and Directory Interface 3. EJB: Enterprise Ja ...
- DataSet离线数据集实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- css样式float造成的浮动“塌陷”问题的解决办法
什么是CSS Float? 定义: float 属性定义元素浮动到左侧或右侧.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级元素,而不论 ...
- struts.xml中的intercepter
1. http://weizhilizhiwei.iteye.com/blog/1005210 Struts2 的核心——拦截器[Interceptor] 2. http://blog.csdn.ne ...
- 156 Useful Run Commands
To Access… Run Command Accessibility Controls access.cpl Accessibility Wizard accwiz Add Hardware Wi ...
- 网络编程Socket之UDP
服务器端实现步骤: 1. 创建 DatagramSocket,指定端口号 2. 创建 DatagramPacket 3. 接收客户端发送的数据信息 4. 读取数据 package cn.jmu.edu ...
- openMPI小集群安装
经过一天的努力,终于完成了openMPI的多节点安装,即小集群安装.本文使用的是openmpi-1.6.5,下载地址见:http://www.open-mpi.org/software/ompi/v1 ...
- mysql的sql优化案例
前言 mysql的sql优化器比较弱,选择执行计划貌似很随机. 案例 一.表结构说明mysql> show create table table_order\G***************** ...
- [Python笔记]第二篇:运算符、基本数据类型
本篇主要内容有:运算符 基本数据类型等 一.运算符 1.算术运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 6.身份运算 7.位运算 8.运算符优先级 二.基本数据类型 1.整数:int ...