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 ...
随机推荐
- python+selenium问题随记
1.用PhantomJS跑程序,莫名遇到有些元素的text不能读取,后来发现,PhantomJS运行时也需要最大化,不是全屏模式的话也和界面浏览器一样会造成压盖无法读取信息,开始以为是PhantomJ ...
- idea获取激活码
访问地址拿到激活码:http://idea.lanyus.com/getkey
- map中使用await 异步函数
let result=await Promise.all(dataComments.map(async (ele)=>{ return (async ()=>{ let resData= ...
- https握手失败案例(一)
OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .read ...
- Mysql如何为表字段添加索引???
1.添加PRIMARY KEY(主键索引): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引) : ALTE ...
- 关于父类中的this指针的问题
在处理一个消息推送的问题的时候遇到个小问题,比如A是B的子类,当A生成实例时,会执行父类的构造函数,那么在父类中,this会是什么类型呢? 于是做了个小测试 子类ChildClass: public ...
- 二级域名绑定ECS
关于阿里云域名的绑定,下面是个人的理解,如有错误请指出. 首先,任何域名都需要在阿里云备案后才可以使用, 如果是二级域名,不能单独备案,需要其顶级域名在阿里云备案. http://help.aliyu ...
- 92.背包问题(lintcode)
注意j-A[i-1]必须大于等于0,只大于0会报错 class Solution { public: /** * @param m: An integer m denotes the size of ...
- Mysql插入中文时提示:ERROR 1366 (HY000): Incorrect string value: '\xE5\x8F\xB0\xE5\xBC\x8F...' fo
Mysql插入数据时提示:ERROR 1366 (HY000): Incorrect string value: ‘\xE5\x8F\xB0\xE5\xBC\x8F…’ fo 分析如下: 首先通过语句 ...
- Bootstrap历练实例:警告样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...