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

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. 配置远程主机http服务器 打包资源

    <1> 搭建nginx  验证nginx是否启动成功 https://blog.csdn.net/wdsdsdsds/article/details/51179780 https://ww ...

  2. nmap的使用

    安装完nmap后,看网上都是直接cmd后nmap的方式来查看是否安装成功,但实际我总是不对,然后自己想着进入安装包执行命令,果然成功.

  3. 在java服务端判断请求是来自哪个终端

    在servlet中,我们可以获取到HttpServletRequest,然后通过HttpServletRequest的getHeader("User-Agent")方法获取请求头中 ...

  4. Java中两个线程是否可以同时访问同一个对象的两个不同的synchronized方法?

    不可以!!! 多个线程访问同一个类的synchronized方法时, 都是串行执行的 ! 就算有多个cpu也不例外 ! synchronized方法使用了类java的内置锁, 即锁住的是方法所属对象本 ...

  5. java【基础】日期操作

    主要是date类,SimpleDateFormat类以及Calendar类的使用. date表示日期,simpleDateFormat 表示日期格式化,Calendar一般用来做时间的操作,比如加减天 ...

  6. kylin 系列(一)安装部署

    kylin 系列(一)安装部署 一.环境说明 1.1 版本选择 cdh 版本下载地址:http://archive.cloudera.com/cdh5/cdh/5/ 软件名称 版本 JDK 1.8 H ...

  7. jquery 表单校验

    <link type="text/css" href="<%=basepath%>css/form/validate.css" rel=&qu ...

  8. assetBundle 中的prefeb资源图片显示粉色方框

    assetBundle打包的资源是有平台属性的,当移动端iOS或者Android AssetBundle资源 在editor 加载的时候,比如TextMeshPro中的字体就不能正确加载 pc端调试, ...

  9. js 定时更改div背景图片

    今天遇到一个业务场景,使用js将一个div标签的背景图片定时更换一下. 之前百度了几个,有css+js,也有css3的,不过css3的兼容有问题,之后同事提示,可以使用js直接来更换div的北京图片, ...

  10. modal 永久移除遮盖层

    样式中直接加入: .modal-backdrop { opacity: 0 !important; filter: alpha(opacity=0) !important;}