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.

题意:给出一个二叉树,判断是不是平衡二叉树

思路:判断左右子树的高度差不超过1

/*** Definition for binary tree* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {boolean balanced = true;private int height(TreeNode root){if (root == null) return 0;if (root.left == null && root.right == null) return 1;int lHeight = height(root.left) + 1;int rHeight = height(root.right) + 1;if (Math.abs(lHeight - rHeight) > 1) balanced = false;return Math.max(lHeight, rHeight);}public boolean isBalanced(TreeNode root) {if (root == null) return true;height(root);return balanced;}
}

LeetCode Balanced Binary Tree相关推荐

  1. leetcode - Balanced Binary Tree

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

  2. leetcode -- Balanced Binary Tree TODO

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

  3. Leetcode::Balanced Binary Tree

    后序遍历,每个节点只遍历一次. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode ...

  4. Leetcode: Balanced Binary Tree

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

  5. LeetCode:Balanced Binary Tree

    题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  6. [LeetCode]Balanced Binary Tree

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

  7. LeetCode 110 Balanced Binary Tree 平衡二叉树

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

  8. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  9. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

最新文章

  1. 如何批量文件格式转化
  2. 重磅直播|SLAM/sfm中的两视图三角化方法
  3. 002_centos7上安装mysql8
  4. ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
  5. pythonzip是迭代器_Python迭代器和zip
  6. 寒风之家 » Thrift压缩
  7. Table.Rows.Remove(dr)和Table.Delete()的区别
  8. C语言用循环结构算平均值,C语言循环结构选择题().doc
  9. 批改网禁止粘贴怎么破_教育部对家长批改作业表态了,明令禁止!你怎么看?...
  10. C++ 执行cmd命令 并获取输出
  11. php 自动切图,前端工程师技能之photoshop巧用系列扩展篇自动切图
  12. C# 调用其他的动态库开发应注意的问题
  13. 程序员这样聊天,让女神疯狂追逐你
  14. python之元组操作
  15. Hystrix入门与分析(一):初识Hystrix
  16. 我的毕业论文————面向对象的软件测试
  17. matlab double 取值,matlab定积分计算结果为表达式,我只想得到数值。用double(ans)也没用,不知道还有没有其他方法!...
  18. Revit二次开发——模型线
  19. c语言课程设计万年历查询,C语言课程设计——万年历.pdf
  20. html边框缩短,有什么办法css border缩短

热门文章

  1. Qt全局热键(windows篇)
  2. maven no Maven Dependencies view
  3. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)
  4. 使用OpenGL绘制圆环体(Torus)
  5. python小项目案例-python_flask小项目实例-编一个小网站
  6. 廖雪峰python教程书-Python 爬虫:把廖雪峰的教程转换成 PDF 电子书
  7. python第三方库下载-3、python第三方库的安装方式
  8. python爬虫吧-Python爬虫如何爬取贴吧内容
  9. python读法1002python读法-Python教程之绘制Mandelbrot集合
  10. 简明python教程pdf-python简明教程中文pdf