题目

平衡二叉树

给定一个二叉树,确定它是高度平衡的。对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过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 : 平衡二叉树的更多相关文章

  1. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  2. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  3. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. [LeetCode] Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. Java数据结构——平衡二叉树的平衡因子(转自牛客网)

    若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...

  7. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  8. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  9. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

随机推荐

  1. AngularJS(16)-路由

    AngularJS 路由 本章节我们将为大家介绍 AngularJS 路由. AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web ...

  2. elr_memory_pool详解

    Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...

  3. 百度的domain命令到底有用吗?

    曾几何时,站长和seoer们在百度输入domain:xxxxxxxx的时候弹出的结果数量让多少人兴奋和失落,为什么我们对百度的domain命令如此着迷呢?因为我们都认为百度的domain命令是查询网站 ...

  4. node.js和socket.io纯js实现的即时通讯实例分享

    在这个例子中,其实node.js并没有真正起到服务器的作用,因为我们这里可以直接运行client.html文件,而不用输入url请求,当 然,要想输入url请求页面内容还需要加入请求静态文件的代码.这 ...

  5. this.IsMounted() is not a function

    I'm trying to build a simple React App. It retrieves data from an ajax call and renders it to the pa ...

  6. struts2结构图

  7. SQLServer处理亿万级别的数据的优化措施

    如何在SQLServer中处理亿万级别的数据(历史数据),可以按以下方面进行: 去掉表的所有索引 用SqlBulkCopy进行插入 分表或者分区,减少每个表的数据总量 在某个表完全写完之后再建立索引 ...

  8. 提高Linux安全性--hosts.allow, hosts.deny 文件修改方法

    有一种办法来提高Linux安全性--修改 hosts.allow , hosts.deny 这2个文件来配置 允许某个ip访问, 或者禁止访问. 可以通过这种方式设置限制 sshd 的远程访问, 只允 ...

  9. django - transaction

    def user_atomic(): User.objects.create(name='purk1', email='pwu1@maxprocessing.com') User.objects.cr ...

  10. opencv初体验

    http://guoming.me/opencv-config  这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...