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

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. R语言-简单模型画图

    1.回归拟合 > plot(mtcars$mpg~mtcars$disp) > lmfit<-lm(mtcars$mpg~mtcars$disp) #线性回归模型 > abli ...

  2. python21期day01笔记总结

    2019.3.27 S21 day01笔记总结 一.计算机基础知识 1.计算机组成 用户 应用软件程序开发——用到了两个方面: 1语法 : 2解释器.编译器.虚拟机: 操作系统的开发 硬件组成 2.操 ...

  3. [leetcode]78. Subsets数组子集

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  4. git 使用遇到的问题

    本博客只记录遇到的问题和解决方案 问题一:git上与本地不同步无法上传 先git pull origin master再git push -u origin master(实在不行或者清空本地,或者清 ...

  5. 回溯算法_ BackTracking

     目前还存在的疑问: 1. 所谓的该分支满足条件之后就回退到上一层节点,可是加谁呢? x[i+1]  ?? 加到 N, 不满足target sum条件就返回上一级(同时改变上一级数为 i+1...纵向 ...

  6. Eigen使用矩阵作为函数参数

    1 使用矩阵作为函数参数介绍 文章来源Writing Functions Taking %Eigen Types as Parameters Eigen为了在函数中传递不同的类型使用了表达式模板技术. ...

  7. laravel config 配置无效

    修改了配置文件config  发现逻辑代码中并无生效. 猜测缓存,所以执行下: php artisan config:cache 缓存文件默认会存在bootstrap/cache 中,并不在stora ...

  8. http协议介绍及get与post请求、响应状态码

    HTTP:  通信双方如果想要通信就必须遵循一定的规则,我们把这个规则称之为HTTP协议! 报文:  HTTP协议通信的内容我们称之为:报文 报文格式:    报文首部 空行 报文主体 1.请求报文 ...

  9. Squid.conf配置详情

    squid常用命令:/usr/local/squid/sbin/squid -z 初始化缓存空间/usr/local/squid/sbin/squid 启动/usr/local/squid/sbin/ ...

  10. Django中的缓存(内存,文件,redis)

    一.Django中的缓存的几种方法 1)单个视图缓存.时间测试 import time from django.views.decorators.cache import cache_page @ca ...