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.

Hide Tags

Tree Depth-first Search

/**
* 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 判断平衡二叉树的更多相关文章

  1. balanced binary tree(判断是否是平衡二叉树)

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

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

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

  3. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

  4. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

  5. LeetCode OJ:Balanced Binary Tree(平衡二叉树)

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

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

    来源:https://leetcode.com/problems/balanced-binary-tree Given a binary tree, determine if it is height ...

  7. LeetCode Balanced Binary Tree (判断平衡树)

    题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...

  8. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

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

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

随机推荐

  1. name和value的简单js用法

    name和value: 例如: <input type="text" name="txt"/> name用于定义这个input收到的值的变量名,在上 ...

  2. SpringCloud微服务实战三:Hystix的基本概念

    1.说到隔离.熔断.降级,最出名的就是 Netflix 开源的 Hystrix 组件,Hystix官方对它描述为:Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止级联故障,在复 ...

  3. python-web-下载所有xkcd漫画

    下载所有xkcd漫画 # downloads every single xkcd comic import requests,os,bs4 url='http://xkcd.com' # start ...

  4. oracle数据导入/导出(2)

    Oracle数据导入导出imp/exp 功能:Oracle数据导入导出imp/exp就相当与oracle数据还原与备份. 大多情况都可以用Oracle数据导入导出完成数据的备份和还原(不会造成数据的丢 ...

  5. vue开发中控制台报错问题

    1.sockjs.js?9be2:1606 GET http://localhost:8566/sockjs-node/info?t=1569478261510 net::ERR_CONNECTION ...

  6. ssm整合:搭建环境

    解决配置中文过滤器后,存入数据库时依旧乱码问题:在web.xml中修改数据库url如下: <property name="jdbcUrl" value="jdbc: ...

  7. 超干货!Cassandra Java堆外内存排查经历全记录

    背景 最近准备上线cassandra这个产品,同事在做一些小规格ECS(8G)的压测.压测时候比较容易触发OOM Killer,把cassandra进程干掉.问题是8G这个规格我配置的heap(Xmx ...

  8. 洛谷 3089 [USACO13NOV]POGO的牛Pogo-Cow

    单调队列优化dp; 对于每个点开个单调队列,按转移到它的点到它的距离从大到小,得分也从大到小排列. 每次枚举当前点前面的所有点,对于每个点的队列中二分一个距离小于等于它到当前点的答案值,放到当前点的队 ...

  9. canvas绘制video

    html <video style="position: relative; object-fit: fill;" preload="auto" id=& ...

  10. 中断描述符表 IDT

    保护模式下三个重要的系统表——GDT.LDT和IDT 这里主要是解释中断描述符表 中断描述符表IDT将每个异常或中断向量分别与它们的处理过程联系起来.与GDT和LDT表类似,IDT也是由8字节长描述符 ...