【Balanced Binary Tree】cpp
题目:
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.
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root)
{
if (!root) return true;
if ( abs(Solution::height(root->left)-Solution::height(root->right)) <= )
{
return Solution::isBalanced(root->left) && Solution::isBalanced(root->right);
}
else
{
return false;
}
}
static int height(TreeNode* p)
{
if (!p) return ;
return max(Solution::height(p->left),Solution::height(p->right))+;
}
};
tips:
第一次刷这leetcode把此题漏掉了。
注意balanced tree是指every node:
(1)判断每个节点left和right深度是否相等
(2)再判断left和right分别是不是balanced的
【Balanced Binary Tree】cpp的更多相关文章
- 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】
平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- 【Maximum Depth of Binary Tree 】cpp
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 【Lowest Common Ancestor of a Binary Tree】cpp
题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
[114-Flatten Binary Tree to Linked List(二叉树转单链表)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
随机推荐
- java IO流——字节流
字节流主要操作byte类型数据,以byte数组为准,主要操作类有InputStream(字节输入流).OutputSteam(字节输出流)由于IputStream和OutputStream都是抽象类, ...
- Selenium入门系列5 默认不显示的下拉列表元素操作
本节课程的下拉框是那种默认隐藏,当鼠标移到菜单上下拉框才显示的.如果直接getelement会报错,提示元素不可见: so,得先让下拉列表显示出来再获取元素 用到的新知识: is_display() ...
- AttributeError: module 'requests' has no attribute 'get' 遇到了这个错误,是因为我把python关键字做了包名。。。
初学者总会犯各种低级错误,这是其一,特此记录.
- CORS跨域限制以及预请求验证
之前我们可以通过“Access-Control-Allow-Origin”,实现跨域请求,那是不是所有跨域请求都可以通过设置Access-Control-Allow-Origin实现跨域请求呢?显然不 ...
- 引用类型(二):Array类型
一.js中的数组与其它语言中的数组的区别1.ECMAScript数组的每一项可以保存任何类型的数据2.ECMAScript数组的大小是可以动态调整的 二.创建数组的基本方式1.使用Array构造函数 ...
- 问题 B: C++习题 对象数组输入与输出
题目描述 建立一个对象数组,内放n(n<10)个学生的数据(学号.成绩),用指针指向数组首元素,输出第奇数(1,3,5,7)个学生的数据. 输入 n和n个学生的学号.成绩 输出 奇数学生的数据 ...
- python_4_interaction
#1(方法1)尽量不用这种拼接法,效率低下,占用内存多 name=input("name:") age=input('age:') job=input('job:') salary ...
- javaweb基础(36)_jdbc进行批处理
在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...
- 破解weblogic(数据库)密码
破解weblogic(数据库)密码所需步骤 注意:本例子本人以本地weblogic为列,必须已经安装weblogic 1.需要问题件 1>.数据源配置文件HKS***-****-jdbc.xml ...
- spring-mybatis整合异常
Failed to read artifact descriptor for XXXXXX:jar:XXXX.RELEAS 原因是maven的本地仓库没有设置好.在别处拷贝过来的项目会有自己的仓库位置 ...