Balanced Binary Tree
判断一棵二叉树是否是平衡树
看到二叉树的题目,基本都可以用递归的思想求解。对于判断是否是一棵平衡树可分为以下几个步骤:
- 计算左子树和右子树的高度差是否大于1,是则返回false
- 判断左子树是否是平衡树,判断右子树是否是平衡树
class Solution {
public:
bool isBalanced(TreeNode *root)
{
if (root == NULL)
return true;
else
{
int left = depth(root->left);
int right = depth(root->right);
if (abs(left - right) > 1)
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
}
int depth(TreeNode* root)
{
if (root == NULL)
return 0;
else
return max(depth(root->left), depth(root->right)) + 1;
}
};
转载于:https://www.cnblogs.com/flyjameschen/p/2afa7e15fcca0b7f32feb58e45ed8cd4.html
Balanced Binary Tree相关推荐
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- leetcode - Balanced Binary Tree
题目:Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a ...
- leetCode 110. Balanced Binary Tree 平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- [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 ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree) 平衡二叉树(Balanced Binary Tree)又称AVL树.AVL 树得名于它的发明者 G. M. Adelson-Velsky 和 E ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- LeetCode Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
最新文章
- mysql数据库主从同步配置教程--数据库同步
- 架设mediawiki服务器续--安装VisualEditor编辑器
- 用正则判断字符串是否为中文的方法
- 第五周课程总结实验报告(三)
- 工作56:element里面表单重置
- 安全公司正在借 AI 之力,预测、防御并抵抗网络攻击
- 老人言 摘自云风的blog
- vue 对象中数组中对象某个属性更改_vue之监听对象、对象数组的改变
- SpringBoot之AOP之基本使用
- mysql system_time_zone乱码_解决mysql设置时区时的错误Unknown or incorrect time zone: 'Asia/Shanghai'...
- mysql输入密码就闪退_MySQL在我的服务列表里凭空消失~
- 如何从Java官网下载 Java API 文档
- .30-浅析webpack源码之doResolve事件流(2)
- Subclipse安装
- 寻找百度图片搜索接口--two
- Quasar的配置文件说明
- python axes3d函数_python3D绘图Axes3D函数详解
- 判断各类无盘软件是否处于超管状态
- 舌尖上的中国第二季整理笔记
- 44岁万达女高管跳楼:摧毁一个中年人有多容易!
热门文章
- 3D溜溜网站用的是php吗_浩辰3D软件中全面的阵列特征你会用吗?快来看看吧
- mysql的count(*)的优化,获取千万级数据表的总行数
- 【django轻量级框架】Django项目导入css,js,images等静态文件
- ajax核心代码提交,ajax表单在Asp.net核心提交后的RedirectToAction
- php 打开报错,php模式下 运行start.php 报错
- 风变编程python第一关脸黑怪我喽_风变编程:Python适合编程初学者学习吗?
- qt怎么做滑动调节参数_冬天冰箱温度怎么调?0到7旋钮是做什么的?学会调节省电又保鲜...
- matlab学习日记,MATLAB学习笔记---DAY1
- grs matlab公式,[转载]matlab中Max的用法(转)
- 想做网络推广浅析网站优化中标题该如何设置?