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.

思考:求二叉树高的变形,加上判断即可。

/*** Definition for binary tree* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
private:bool flag;
public:int DFS(TreeNode *root){if(root){int height1=DFS(root->left);int height2=DFS(root->right);if(abs(height1-height2)>1) flag=false;return max(height1,height2)+1;}else return 0;}bool isBalanced(TreeNode *root) {flag=true;int height=DFS(root);//树高return flag;}
};

  

转载于:https://www.cnblogs.com/Rosanna/p/3463596.html

[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

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

  3. leetcode -- Balanced Binary Tree TODO

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

  4. Leetcode::Balanced Binary Tree

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

  5. Leetcode: Balanced Binary Tree

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

  6. LeetCode:Balanced Binary Tree

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

  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. Nginx负载均衡与配置Nginx的ssl
  2. canvas 圆角矩形填充_Canvas:随机长方形生成和色块填充
  3. PKI/CA (2)核心说明
  4. Object类的hashCode()方法
  5. controller配对与接触配对
  6. php判断一个字符串是否为纯数字,php判断变量是否为纯数字字符串的方法
  7. 黑客帝国动态特效代码
  8. 前端车牌识别SDK算法及原理
  9. 微信认证小程序如何绑定微信支付商户平台
  10. Python:整数的阶乘
  11. win7计算机节电模式,大师详解win7省电模式怎么关的方法介绍
  12. English-旅游英语及情景对话
  13. cve-2021-22205复现
  14. C语言编周期100ms的方波信号,单片机系统设计 - 2020学年春(邸志刚)-中国大学mooc-题库零氪...
  15. 【转】高级育婴师 告诉你,一定要这么做
  16. Put GridView inside ScrollView
  17. 六款Android 应用的自动化测试工具
  18. 苹果、天猫同步关停是因为发新品?这些猜想你看靠谱吗....
  19. Linux使用技巧1--挂载U盘和Windwos分区
  20. TCP的三次握手与四次挥手理解及面试题(很全面)

热门文章

  1. 监控某个dll被修改_浅谈动力环境监控系统技术标准
  2. Arcgis执行Raster Project时报Error001143 : Background server threw an exception
  3. 前端开发入门站点推荐
  4. kubernetes 客户端client-go 使用及常用api
  5. Intellij IDEA 添加jar包的三种方式
  6. Pixel Bender 浅尝
  7. SNS应用开发架构建议
  8. 深入理解kafka_深入理解Kafka随书代码示例已整理完毕,附地址~
  9. k8s service type_通过搭建MySQL掌握k8s(Kubernetes)重要概念(上):网络与持久卷...
  10. 如何卸载mysql5.6.28_如何完全删除MySQL以进行全新安装