lintcode : 平衡二叉树
题目
平衡二叉树
给定一个二叉树,确定它是高度平衡的。对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1。
给出二叉树 A={3,9,20,#,#,15,7}
, B={3,#,20,15,7}
A) 3 B) 3
/ \ \
9 20 20
/ \ / \
15 7 15 7
二叉树A是高度平衡的二叉树,但是B不是
解题
递归求高度
判断左右孩子高度之差是小于等于1,这里还是需要递归的
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public boolean isBalanced(TreeNode root) {
// write your code here
if(root == null)
return true;
int lft = depth(root.left);
int rit = depth(root.right);
if(Math.abs(lft-rit)<=1){
return isBalanced(root.left)&&isBalanced(root.right);
}
else
return false; }
public int depth(TreeNode root){
if(root ==null)
return 0;
if(root.left==null && root.right ==null)
return 1;
int lft = depth(root.left);
int rit = depth(root.right);
return Math.max(lft,rit) + 1;
}
}
Java Code
lintcode : 平衡二叉树的更多相关文章
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java数据结构——平衡二叉树的平衡因子(转自牛客网)
若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
随机推荐
- jquery 从页面获取li数组,删除不在数组中的key
应用场景: 获取页面 li 下面 key的值,添加到 arr数组 删除车型不在arr 数组中的value值. 示例代码: var getSaleModels = function(brand_id){ ...
- ArcGIS API for JavaScript介绍
ArcGIS API for JavaScript中的类是按照模块组织的,主要包含esri.esri/geometry.esri/renderers.esri/symbols.esri/symbols ...
- 读取iis日志到sql server
using Fasterflect; using System; using System.Collections.Generic; using System.Data.SqlClient; usin ...
- 安装node和grunt
由于我用的恶事win8的系统,所以在安装nodeJS的时候出现了2503和2502的错误.解决方案: 进入cmd,然后输入msiexec /package [路径:为你将要安装的node所在的位置] ...
- String与StringBuffer对象问题
下面的代码创建了三个String对象,其中pool中一个,heap中两个 String s1 = new String("abc"); String s2 = new String ...
- TDirectory.CreateDirectory 完整、严谨的创建一个目录
描述:创建一个目录,不包含多级目录(多级目录使用System.SysUtils.ForceDirectories,Vcl.FileCtrl.ForceDirectories已过时) procedure ...
- WPF之Binding对数据的转换(第五天)
Binding在Slider控件与TextBox控件之间建立关联,值可以互相绑定,但是它们的数据类型是不同的,Slider是Double类型,Text为String.原来,Binding有一种机制称为 ...
- .NET Framework 3.5 安装错误:0x800F0906、0x800F081F、0x800F0907
使用Add-WindowsFeature 照成的问题 I get the failure below.. If I pick the Server 2012 R2 image from 8/15/2 ...
- erp与电子商务集成的结构图
集约化采购管理系统和电子商务平台统一规划.统一设计,通过系统之间的安全接口全面集成,进而实现资源共享和数据共享,企业内外部系统运作的一体化,建立企业同上.下游合作伙伴的电子数据交互,从而提高电子商务的 ...
- using System.Threading.Tasks;
using System.Threading.Tasks; .Net并行库介绍——Task1