题目:

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.

题目解答:判断一棵给定的二叉树是不是平衡二叉树。平衡二叉树的条件有:

    它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

对于树的处理,一般都使用递归的方式。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root == NULL)
            return true;
        if((root -> left == NULL ) && (root -> right == NULL))
            return true;
        int leftDepth = 0;
        int rightDepth = 0;
        return isSubBalance(root -> left,leftDepth) && isSubBalance(root -> right , rightDepth) && (abs(leftDepth - rightDepth) <= 1) ;
    }
   
    bool isSubBalance(TreeNode *root,int &depth)
    {
        if(root == NULL)
        {
            depth = 0;
            return true;
        }
        if((root -> left == NULL ) && (root -> right == NULL))
        {
            depth = 1;
            return true;
        }
        else
        {
            depth = 1;
            int leftDepth = 0;
            int rightDepth = 0;
            return isSubBalance(root -> left,leftDepth) && isSubBalance(root -> right , rightDepth) && (abs(leftDepth - rightDepth) <= 1) && (depth += max(leftDepth,rightDepth) ) ;
        }
    }
   
};

转载于:https://www.cnblogs.com/CodingGirl121/p/5425720.html

Leetcode题目:Balanced Binary Tree相关推荐

  1. LeetCode 110 Balanced Binary Tree

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

  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

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

  4. 【leetcode】Balanced Binary Tree(middle)

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

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

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

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

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...

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

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

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

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

  9. leetcode - Balanced Binary Tree

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

  10. LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

最新文章

  1. 【转】apache常用配置
  2. R语言如何将字符串转变为命令执行
  3. 设计模式 — 行为型模式 — 策略模式
  4. 强/若类型语言 动/静态语言
  5. Android短信发送流程之普通短信发送(原)
  6. nginx核心模块常用指令
  7. 安卓学习-WebView
  8. 数据结构期末复习之选择排序
  9. 流利说公布上市后首份财报:第三季净收入1.8亿
  10. 使用静态库的一些问题 -all_load
  11. FFMPEG 常用命令行
  12. paip.网站提示SESSION过期 登录过期 以及二次登录的问题
  13. html 中加载字体太慢,css字体文件包太大无法引入怎么处理?
  14. 双十一数码好物指南,双十一不踩坑推荐
  15. 创建一个urdf机器人_ROS机器人Diego制作16-创建机器人的urdf模型描述文件
  16. 计算机开机慢更新配置,系统开机慢?教你如何提高电脑开机速度!
  17. 20135220谈愈敏-期末总结
  18. 企业上云业务系统上云是怎么回事儿?
  19. php tree view,VB.Net树视图(TreeView)
  20. 调用notifyDataSetChanged无法更新ListView

热门文章

  1. 读书笔记之MySQL技术内幕
  2. Linux中history历史命令使用方法详解
  3. free -m 内存
  4. Linux中的 inode以及 软硬链接分析
  5. MegaCli查看RAID
  6. WinDriver的一些
  7. 根据开发提供的svn更新版本号从开发分支合并代码到测试分支工作目录的部分脚本...
  8. php实现适配器模式(转)
  9. Android recipe 在代码中写布局
  10. 信息化监理公司的所有问题归到底是人的使用和管理