题目

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.

题解

采用递归的方法,要记录depth用来比较。

代码如下:

 1     public int checkBalanced(TreeNode t){
 2         if(t==null)
 3             return 0;
 4             
 5         int leftheight = checkBalanced(t.left);
 6         if(leftheight == -1)
 7             return -1;
 8         
 9         int rightheight = checkBalanced(t.right);
         if(rightheight == -1)
             return -1;
         
         if(Math.abs(leftheight-rightheight)>1)
             return -1;
         else
             return Math.max(leftheight,rightheight)+1;
     }
     
     public boolean isBalanced(TreeNode root) {
         if(checkBalanced(root) == -1)
             return false;
         else
             return true;
     }

Balanced Binary Tree leetcode java的更多相关文章

  1. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  2. 110.Balanced Binary Tree Leetcode解题笔记

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

  3. Balanced Binary Tree(Java代码没有结束,是什么原因???)

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

  4. Balanced Binary Tree [LeetCode]

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

  5. Balanced Binary Tree——LeetCode

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

  6. Minimum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  7. Maximum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  8. LeetCode算法题-Balanced Binary Tree(Java实现)

    这是悦乐书的第167次更新,第169篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第26题(顺位题号是110).给定二叉树,判断它是否是高度平衡的.对于此问题,高度平衡二 ...

  9. Balanced Binary Tree --Leetcode C++

    递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...

随机推荐

  1. [CodeForces]CodeForces 13D 几何 思维

    大致题意: 给出N个红点和M个蓝点,问可以有多少个红点构成的三角形,其内部不含有蓝点 假设我们现在枚举了一条线段(p[i],p[j]),我们可以记录线段下方满足(min(p[i].x,p[j].x)& ...

  2. CTF Writeup 一个专门收集WP的网站

    www.ctfwp.com 创建于2019-04-15 致力于收集网上公开writeup,方便大家学习.

  3. MIT-6.828-JOS-lab1:C, Assembly, Tools, and Bootstrapping

    Lab1:Booting a PC 概述 本文主要介绍lab1,从内容上分为三部分,part1简单介绍了汇编语言,物理内存地址空间,BIOS.part2介绍了BIOS从磁盘0号扇区读取boot loa ...

  4. Java 中的 I/O

    I/O 指的是 input 和 output ,也就是输入和输出,我们说的是 Java 中的 I/O,那我们就在站在虚拟机的角度去看看有哪些输入和输出.输入又可以称为数据源端,能想到的会有,文件,网络 ...

  5. 用户管理和FTP服务配置

    批量创建用户 python脚本:creuser.py import osulist=open('usernames','r')for x in ulist: cmd="useradd -g ...

  6. ZOJ2112 BZOJ1901 Dynamic Rankings 树套树 带修改的区间第k小

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 树套树,线段树套splay或者主席树套树状数组,我抄了一下hzwer ...

  7. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  8. BZOJ.1032.[JSOI2007]祖码(区间DP)

    题目链接 BZOJ 洛谷 AC代码: 区间DP,f[i][j]表示消掉i~j需要的最少珠子数. 先把相邻的相同颜色的珠子合并起来. 枚举方法一样,处理一下端点可以碰撞消除的情况就行. 当然合并会出现问 ...

  9. Codeforces Round #461 (Div. 2)

    A - Cloning Toys /* 题目大意:给出两种机器,一种能将一种原件copy出额外一种原件和一个附件, 另一种可以把一种附件copy出额外两种附件,给你一个原件, 问能否恰好变出题目要求数 ...

  10. 【平面图最小割】BZOJ2007-[NOI2010]海拔

    [题目大意] 城市被东西向和南北向的主干道划分为n×n个区域,包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向道路.现得到了每天每条道路两个方向的人流量.每一个交叉路口都有海拔,每向上爬h ...