判断一棵二叉树是否是平衡树

看到二叉树的题目,基本都可以用递归的思想求解。对于判断是否是一棵平衡树可分为以下几个步骤:

  1. 计算左子树和右子树的高度差是否大于1,是则返回false
  2. 判断左子树是否是平衡树,判断右子树是否是平衡树
  1. class Solution {
  2. public:
  3. bool isBalanced(TreeNode *root)
  4. {
  5. if (root == NULL)
  6. return true;
  7. else
  8. {
  9. int left = depth(root->left);
  10. int right = depth(root->right);
  11. if (abs(left - right) > 1)
  12. return false;
  13. return isBalanced(root->left) && isBalanced(root->right);
  14. }
  15. }
  16. int depth(TreeNode* root)
  17. {
  18. if (root == NULL)
  19. return 0;
  20. else
  21. return max(depth(root->left), depth(root->right)) + 1;
  22. }
  23. };
来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/flyjameschen/p/2afa7e15fcca0b7f32feb58e45ed8cd4.html

Balanced Binary Tree相关推荐

  1. 【leetcode】Balanced Binary Tree(middle)

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

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

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

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

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

  4. leetcode - Balanced Binary Tree

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

  5. leetCode 110. Balanced Binary Tree 平衡二叉树

    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

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

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

    平衡二叉树(Balanced Binary Tree) 平衡二叉树(Balanced Binary Tree)又称AVL树.AVL 树得名于它的发明者 G. M. Adelson-Velsky 和 E ...

  9. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

  10. LeetCode Balanced Binary Tree

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

最新文章

  1. mysql数据库主从同步配置教程--数据库同步
  2. 架设mediawiki服务器续--安装VisualEditor编辑器
  3. 用正则判断字符串是否为中文的方法
  4. 第五周课程总结实验报告(三)
  5. 工作56:element里面表单重置
  6. 安全公司正在借 AI 之力,预测、防御并抵抗网络攻击
  7. 老人言 摘自云风的blog
  8. vue 对象中数组中对象某个属性更改_vue之监听对象、对象数组的改变
  9. SpringBoot之AOP之基本使用
  10. mysql system_time_zone乱码_解决mysql设置时区时的错误Unknown or incorrect time zone: 'Asia/Shanghai'...
  11. mysql输入密码就闪退_MySQL在我的服务列表里凭空消失~
  12. 如何从Java官网下载 Java API 文档
  13. .30-浅析webpack源码之doResolve事件流(2)
  14. Subclipse安装
  15. 寻找百度图片搜索接口--two
  16. Quasar的配置文件说明
  17. python axes3d函数_python3D绘图Axes3D函数详解
  18. 判断各类无盘软件是否处于超管状态
  19. 舌尖上的中国第二季整理笔记
  20. 44岁万达女高管跳楼:摧毁一个中年人有多容易!

热门文章

  1. 3D溜溜网站用的是php吗_浩辰3D软件中全面的阵列特征你会用吗?快来看看吧
  2. mysql的count(*)的优化,获取千万级数据表的总行数
  3. 【django轻量级框架】Django项目导入css,js,images等静态文件
  4. ajax核心代码提交,ajax表单在Asp.net核心提交后的RedirectToAction
  5. php 打开报错,php模式下 运行start.php 报错
  6. 风变编程python第一关脸黑怪我喽_风变编程:Python适合编程初学者学习吗?
  7. qt怎么做滑动调节参数_冬天冰箱温度怎么调?0到7旋钮是做什么的?学会调节省电又保鲜...
  8. matlab学习日记,MATLAB学习笔记---DAY1
  9. grs matlab公式,[转载]matlab中Max的用法(转)
  10. 想做网络推广浅析网站优化中标题该如何设置?