LeetCode(110) Balanced Binary Tree
题目
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,只需要求得左右子树的高度,比较判定即可。
AC代码
class Solution {
public:
//判断二叉树是否平衡
bool isBalanced(TreeNode* root) {
if (!root)
return true;
int lheight = height(root->left), rheight = height(root->right);
if (abs(lheight - rheight) <= 1)
return isBalanced(root->left) && isBalanced(root->right);
else
return false;
}
//求树的高度
int height(TreeNode *root)
{
if (!root)
return 0;
else
return max(height(root->left), height(root->right)) + 1;
}
};
LeetCode(110) Balanced Binary Tree的更多相关文章
- LeetCode(24)-Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode(114) Flatten Binary Tree to Linked List
题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...
- 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 ...
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- LeetCode(226)Invert Binary Tree
题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- leetcode笔记(二)94. Binary Tree Inorder Traversal
题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...
- LeetCode(99) Recover Binary Search Tree
题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...
- LeetCode(98) Validate Binary Search Tree
题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...
随机推荐
- 详解JS作用域(一)
一.什么是作用域 存储和访问变量,是任何一种编程语言最基本的功能之一,变量存在哪里?程序需要时如何找到它?这些问题需要一套良好的规则来规范,这套规则,就成为作用域. 二.编译原理 js通常归类为解释语 ...
- sourceTree免注册免登陆使用方法-Windows
安装sourceTree需要注册Google账号,而现在国内注册账号需要FQ,超级麻烦,所以还是免注册的号. 处理方法: 解决办法 在目录C:\Users\{youruser}\AppData\Loc ...
- P1051 谁拿了最多奖学金——水题
题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1 ...
- JQ中的问题
$(function(){$(document).bind("click", function (e) {$(e.target).closest("p").cs ...
- DVWA之文件包含(File inclusion)
daFile inclusion的意思就是文件包含,当服务器开启了allow_url_include选项时,就可以通过PHP的某些特征函数 include,require和include_once,r ...
- TitleBar(标题栏)的使用
github地址:https://github.com/buhuiming/BHMAndroid 使用compile 'com.bhm.sdk.bhmlibrary:BHMLibrary:1.1.2' ...
- idea 设置加载多个资源文件,显示本地图片
idea 经常只会设置一个资源路径,这个路径就是项目的路径.但是当要加载的文件处于其他位置时,则需要增加虚拟路径的配置. 如图:第一个是项目路径 第二个是图片路径
- Hibernate:Disjunction&Conjunction构造复杂的查询条件.
Hibernate:Disjunction&Conjunction构造复杂的查询条件 Disjunction和Conjunction是逻辑或和逻辑与,如下: 用来组合一组逻辑或[or]条件的方 ...
- PHP 根据两点的经纬度计算距离
/** * @name 根据经纬度确定两点的距离[地理位置] * @author tbj * @param float $lat 纬度值 * @param float $lng 经度值 * @date ...
- IOS小技巧整理
1 随机数的使用 头文件的引用 #import <time.h> #import <mach/mach_time.h> srandom()的使用 ...