题目:
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。

分析:
这其实就是判断是否是平衡二叉树。
平衡二叉树,又称AVL树。它或者是一棵空树,或者是具有下列性质的二叉树:它的左子树和右子树都是平衡二叉树,且左子树和右子树的高度之差之差的绝对值不超过1。
下面的做法,递归遍历每个节点,每次递归,都获取左右子树的高度进行判断,采用-1作为已经检测到不平衡的标志位,其余情况返回当前子树的高度,用作上一层递归判断。

代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {public boolean isBalanced(TreeNode root) {int depth=getDepth(root,0);return depth!=-1;//-1代表子树出现不平衡}public int getDepth(TreeNode root,int length){if(root==null){return length;}int leftDepth=getDepth(root.left,length+1);int rightDepth=getDepth(root.right,length+1);if(leftDepth==-1||rightDepth==-1||leftDepth-rightDepth>1||rightDepth-leftDepth>1){return -1;}return leftDepth>rightDepth?leftDepth:rightDepth;}
}

Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]相关推荐

  1. Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树所有左右子数 ...

  2. Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  3. Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  4. Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  5. Leet Code OJ 326. Power of Three [Difficulty: Easy]

    题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  6. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  7. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

    题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  8. Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]

    题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursiv ...

  9. Leet Code OJ 38. Count and Say [Difficulty: Easy]

    题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...

最新文章

  1. Hadoop实战第四章--读书笔记
  2. 清除XCode缓存和生成文件
  3. 反思耗时任务异步处理
  4. itextsharp c# asp.net 生成 pdf 文件
  5. PAT乙级 1005继续3n+1猜想
  6. 6位大师浅谈未来三年大数据的发展
  7. 拒绝干扰 解决Wi-Fi的最大问题
  8. 斗鱼连接弹幕Demo_pythonC#
  9. LINUX使用chrpath更改编译结果的rpath
  10. 根据select的内容来批量修改一个表的字段
  11. C语言程序设计:图书管理系统(附代码)
  12. NOIP2013 复盘
  13. SPSS 24 安装详细教程及下载
  14. 运动控制第一篇之直流电动机建模
  15. highchart图表drilldown钻取功能及event点击事件添加(1)
  16. 企业高薪招人,近5成岗位月薪过万
  17. AUTOSAR CAN Wakeup Analysis
  18. 游戏开发入门(十)游戏中的网络模块
  19. GDrive is here
  20. 兴业数金java开发笔试+一面

热门文章

  1. mybatis获取mysql存储过程out参数的值_mybatis接受mysql存储过程out的值
  2. L1-048. 矩阵A乘以B
  3. POJ1269(判断线段相交)
  4. 1.const关键字.rs
  5. 【Boost】boost库asio详解5——resolver与endpoint使用说明
  6. Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
  7. 带你深入理解分布式事务,掌握后台分布式核心技术,PS:送5本!
  8. 高薪进大厂 | 面试指南
  9. 干掉 Swagger,试试这个!
  10. 以“用户播放行为与体验”为核心的视频服务质量优化