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

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int depth(TreeNode *node){
  13. if(node==NULL)
  14. return ;
  15. int i=depth(node->left);
  16. int j=depth(node->right);
  17. return max(i,j)+;
  18. }
  19. bool isBalanced(TreeNode *root) {
  20. if(root==NULL)
  21. return true;
  22. int leftDep=depth(root->left);
  23. int rightDep=depth(root->right);
  24. if(abs(leftDep-rightDep)<=)
  25. return isBalanced(root->left) && isBalanced(root->right);
  26. else
  27. return false;
  28. }
  29. };

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. springboot 集成eureka 超详细配置

    撸了今年阿里.头条和美团的面试,我有一个重要发现.......>>> 原文链接: https://blog.csdn.net/nanbiebao6522/article/detail ...

  2. mysql 对数据库操作的常用sql语句

    1.查看创建某个数据库的 创建语句 show create database mysql 这个sql语句的意思是 展示创建名为mysql的数据库的 语句.执行之后如下图所示 仿造上面这个创建语句 创建 ...

  3. kafka理论

    一.消息队列,简称MQ,message queue 生产者:生存数据写到kafka,持久化到硬盘.对同一个Topic来讲,生产者通常只有‘一个’(可以多并发)数据保存时常可以配置,默认保存七天. 消费 ...

  4. CentOS + Nginx 的常用操作指令总结

    CentOS + Nginx 的常用操作指令总结 一. 关于CentOS 查看 yum 源是否存在 yum list | grep nginx 如果不存在 或者 不是自己想要的版本 可以自己设置Ngi ...

  5. android 数据绑定(1)Ativity、Fragment、Item绑定数据源

    1.简介 官方文档:  https://developer.android.com/topic/libraries/data-binding 官方示例: https://github.com/andr ...

  6. bzoj 4373 算术天才⑨与等差数列——线段树+set

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4373 能形成公差为k的等差数列的条件:mx-mn=k*(r-l) && 差分 ...

  7. light7结合jquery实现开关按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. node学习记录之res,req处理方法

    上一篇中,我们讲述了怎么去用node搭建一个服务器环境,然后设置路由 在路由中我们用了一些方法,req.query("id") , res.end() , res.send()这三 ...

  9. LintCode 合并两个排序

    将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回1->2->3->8-> ...

  10. 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]

    P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...