给定一个二叉树,确定它是高度平衡的。
对于这个问题,一棵高度平衡二叉树的定义是:
一棵二叉树中每个节点的两个子树的深度相差不会超过 1。
案例 1:
给出二叉树 [3,9,20,null,null,15,7]:
    3
   / \
  9  20
    /  \
   15   7
返回 true 。
案例 2:
给出二叉树 [1,2,2,3,3,null,null,4,4]:
       1
      / \
     2   2
    / \
   3   3
  / \
 4   4
返回 false 。
详见:https://leetcode.com/problems/balanced-binary-tree/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private boolean isBalanced=true;
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
getDepth(root);
return isBalanced;
}
private int getDepth(TreeNode root){
if(root==null){
return 0;
}
int left=getDepth(root.left);
int right=getDepth(root.right);
if(Math.abs(left-right)>1){
isBalanced=false;
}
return left>right?left+1:right+1;
}
}

110 Balanced Binary Tree 平衡二叉树的更多相关文章

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

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

  2. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

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

  3. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  4. 110. Balanced Binary Tree - LeetCode

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

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

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

  6. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  7. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  8. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  9. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

随机推荐

  1. SSD Network Architecture--keras version

    这里的网络架构和论文中插图中的网络架构是相一致的.对了,忘了说了,这里使用的keras版本是1.2.2,等源码读完之后,我自己改一个2.0.6版本上传到github上面.可别直接粘贴复制,里面有些中文 ...

  2. bootstrap 学习笔记(1)---介绍bootstrap和栅格系统

    学习前端许久,对于布置框架和响应浏览器用html 和javascript 写的有点繁琐,无意间看到这个框架,觉得挺好用的就开始学习了,但是这个框架上面有很多知识,不是所有的都要学的,故将学习笔记和觉得 ...

  3. vim的tab缩进及用空格设置

    编辑~/.vimrc文件,分别设置用空格而不是用tab,一个tab多少个空格,自动缩进多少宽度,显示行号. set expandtabset tabstop=4 set shiftwidth=4 se ...

  4. mtk6737t摄像头配置文件的编译

    修改摄像头的配置文件后,一直没有编译生效,要make一遍才生效,最终查出编译配置的方法摄像头配置文件路径 vendor/mediatek/proprietary/custom/mt6735/hal/D ...

  5. 五、hibernate在myeclipse中生成实体和映射

  6. Lecture 0 --基本说明

    Abstract:本章所记录的知识是后面章节需要的前导知识,请务必仔细读本章,理解涉及的知识点,方便后面章节的学习.本次数据结构笔记主要参考殷人昆的<数据结构(C语言描述)>这本书,需要的 ...

  7. Linux 无法登陆172.***.***.***的子网

    1. sudo dhclient -r 这条命令重复执行几次 2. dhclient - 3.查看ifconfig

  8. SQL 系统表

    http://www.cnblogs.com/asdcer/archive/2007/05/14/746377.aspx

  9. ECMAScript 6 &ECMAScript 5(在线手册)

    https://www.w3.org/html/ig/zh/wiki/ES5#.E8.A1.A8.E8.BE.BE.E5.BC.8F      ECMAScript 5(在线手册) http://es ...

  10. Qt Creator Theme FlatDark 配色

    1.预处理指令,宏定义 颜色 #FF6AAD 2.普通代码 颜色 #D6CF9A 3.头文件 #D69545 4.系统限定符(namespace, class, public, typedef等)  ...