要求:判断一棵树是否是平衡二叉树

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 .
 

代码如下:

 struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x),left(NULL), right(NULL) {}
}; int maxTreeDepth(TreeNode *root) //先求树的深度
{
if (NULL == root)
return ;
int lval = maxTreeDepth(root->left);
int rval = maxTreeDepth(root->right);
return + (lval > rval ? lval:rval);
}
bool isBalanced(TreeNode *root)//根据树的深度再来判断是否是平衡树
{
if(NULL == root)
return true;
int diff = maxTreeDepth(root->left) - maxTreeDepth(root->right);
if (diff < - || diff > )
return false;
return isBalanced(root->left) && isBalanced(root->right);
}

LeetCode:110_Balanced Binary Tree | 平衡二叉树 | Easy的更多相关文章

  1. [LeetCode] Balanced Binary Tree 平衡二叉树

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

  2. [Leetcode] Balanced binary tree平衡二叉树

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

  3. [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  4. [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 ...

  5. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  6. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  7. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  8. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  9. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

随机推荐

  1. elasticsearch template

    # curl -XPUT localhost:9200/_template/template_1 -d '{"template" : "te*","s ...

  2. 合并数组,改变原数组apply与不改变原数组

    一看见合并数组,可能第一反应就是concat,concat确实具有我们想要的行为,但它实际上并不附加到现有数组,而是创建并返回一个新数组. 同样你也许会想到ES6的扩展运算符...         但 ...

  3. flash/flex builder在IE中stage.stageWidth始终为0的解决办法

    这应该是IE的bug,解决办法: 原作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com stage.align=StageAlign.TOP_LEFT; stage.scal ...

  4. np.cumsum()函数和正则表达式的含义

  5. MYSQL 时间类型

    常见四种:DATE, TIME, DATETIME, TIMESTAMP DATE: 只表示年月日,YYYY-MM-DD TIME: 只表示时分秒,HH-mm-SS DATETIME: DATE和TI ...

  6. 337. House Robber III二叉树上的抢劫题

    [抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  7. 网站日志流量分析采集(LuaJIT系统环境部署-node03,相关jar包自己手动上传)

    注:/usr/local/src 是源码包路径,可以自己更改 服务器中安装依赖 yum -y install gcc perl pcre-devel openssl openssl-devel 上传 ...

  8. ABP框架系列之二十七:(Feature-Management-特征管理)

    Introduction Most SaaS (multi-tenant) applications have editions (packages) those have different fea ...

  9. s2 Docker环境的快速搭建方法

    常规linux下安装 centos7 下配置docker源并安装 cat >/etc/yum.repos.d/docker.repo< [dockerrepo] name=Docker R ...

  10. C#字符串操作方法签名等

    class Program { /// <summary> /// C# 里Main方法不需要public,而且不允许有两个是Main(string[] args)[包括String[] ...