题目

平衡二叉树

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

    1 ubuntu中如何将终端添加到右键 /home/cui/.local/share/nautilus/scripts #!/bin/bash #cd $NAUTILUS_SCRIPT_CURRENT ...

  2. JS定时器实例解析

    在javascritp中,有两个关于定时器的专用函数. 分别为:1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...

  3. 注意:php5.4删除了session_unregister函数

    在php5.4版本中,应经删除了session_unregister该函数.朋友们注意一下 前几天安装了dedecms系统,当在后台安全退出的时候,后台出现空白,先前只分析其他功能去了,也没太注意安全 ...

  4. 用golang启动一个daemon

    用golang启动一个daemon package main import ( "fmt" "log" "os" "runtime ...

  5. spring的配置

    web.xml的配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...

  6. Python学习第五天

    复习内容: · 迭代器&生成器 · 装饰器 · Json & pickle 数据序列化 · 软件目录结构规范yi 一.生成器 1.   列表生成式: 2.   生成器的定义:在Pyth ...

  7. allegro生成光绘文件时,通过cam打开,*.drl钻孔文件不识别,为Unknow类型

    生成钻孔文件时,NC_Parameters中,应该选Absolute

  8. php正则表达式判断是否为ip格式

    <?php $a = '127.0.0.111'; $b = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/",$a); ...

  9. centos crontab 定时任务详解

    安装crontab: yum install crontabs 说明: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 ...

  10. 网络笔记01-2 scoket

    scoket: 1.socket /** 第一个参数(domain): 表示用什么协议 AF_INET 为IPV4开发 第二个参数(type): 表示scoket为什么类型SOCK_STREAM为TC ...