Balanced Binary Tree 判断平衡二叉树
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 {
public:
int depth(TreeNode *node){
if(node==NULL)
return ;
int i=depth(node->left);
int j=depth(node->right);
return max(i,j)+;
}
bool isBalanced(TreeNode *root) {
if(root==NULL)
return true;
int leftDep=depth(root->left);
int rightDep=depth(root->right);
if(abs(leftDep-rightDep)<=)
return isBalanced(root->left) && isBalanced(root->right);
else
return false;
}
};
Balanced Binary Tree 判断平衡二叉树的更多相关文章
- balanced binary tree(判断是否是平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- LeetCode OJ:Balanced Binary Tree(平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree(平衡二叉树)
来源:https://leetcode.com/problems/balanced-binary-tree Given a binary tree, determine if it is height ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- css背景属性整理
背景颜色 {background-color:red}/*常用十六进制颜色#fff*/ 图片 {background-image:url();} /*插入图片路径*/ 重复 {background-r ...
- HTML5:使用Canvas和Input range控件放大缩小图片,剪裁,并上传图片
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- VirtualBox新建虚拟机常用配置
1.内存给到1G,不然无法进入图形界面进行分区配置及软件包选择.虚拟机存储记得放到D盘. 2.网络选择桥接模式以便SSH,初次登陆后配置/etc/sysconfig/network-scripts/i ...
- 差分进化算法(DE)的C++面向对象方法实现
代码来源于网络,写得非常棒 /*DE_test *对相应的Matlab程序进行测试 */ #include <iostream> #include <cmath> #inclu ...
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- 20190922-雅礼Day2
先送大家几个变量名: 具体的可以去$C++ \ Reference$里看(本页 右侧/下侧 有链接) 或者等一下奇迹银桥第三氮 const int c; mutable int a; volatile ...
- beego应用做纯API后端如何使用jwt实现无状态权限验证
jwt是什么,可以百度下其它文章,我原来看到一个讲的详细的,现在找不到了.先简单介绍下我个人的理解,就是一个token,只不过通过加密解密的手段,能让这一串字符带有一些简单的信息.这样解密jwt后不用 ...
- IntelliJ IDEA 17 创建maven项目
参考博客: https://yq.aliyun.com/articles/111053# 部署服务器时 没有Tomcat Server选项
- 数据库----SQL基本查询
SQL基本查询 查表 :show create table 表名(show tables); describe 表名:(desc 表名)// 模糊查询:show table like '%c%';(查 ...
- Linux下读写UART串口的代码
Linux下读写UART串口的代码,从IBM Developer network上拿来的东西,操作比較的复杂,就直接跳过了,好在代码能用,记录一下- 两个实用的函数- //////////////// ...