LeetCode110 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.(Easy)
分析:
利用辅助函数求深度,然后根据heighted-balanced定义判定即可
代码:
class Solution {
int depth(TreeNode* root) {
if (root == nullptr) {
return ;
}
return max(depth(root -> left), depth(root -> right)) + ;
}
public:
bool isBalanced(TreeNode* root) {
if (root == nullptr) {
return true;
}
int left = depth(root -> left);
int right = depth(root -> right);
if (abs(left - right) <= && isBalanced(root -> left) && isBalanced(root -> right)) {
return true;
}
return false;
}
};
LeetCode110 Balanced Binary Tree的更多相关文章
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- [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 ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
随机推荐
- 一次完整的HTTP事务的过程、从输入URL到网页展示,浏览器都经历了什么?
详细介绍:老生常谈-从输入url到页面展示到底发生了什么 (1)一次完整的HTTP事务的过程 基本流程: a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服务 ...
- 「LOJ10104」「一本通 3.6 练习 5」Blockade-Tarjan
首先%%%lydrainbowcat 这个题唔,真是Tarjan好题(?) 不知道为啥也叫BLO,首字母? <题面> 搜索树真真的棒! 仔细看看,知道是Tarjan了,切掉一个点有不通的对 ...
- Lowest Common Ancestor (LCA)
题目链接 In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defi ...
- Hackerrank--Emma and sum of products (FFT)
题目链接 Emma is really fond of integers and loves playing with them. Her friends were jealous, and to t ...
- 【solr】schemaFactory配置相关schema.xml
schemaFactory配置相关schema.xml 关于schemaFactory的配置困扰我半天啦,下面来总结一下. 话说,好像是从5.0以后就已经没有schema.xml啦,这是由于Solr ...
- [jnhs]未完待续HttpServletRequest示例
uri: /kaihu/showip.html method: GET QueryString: null Parameters: Headers: Name: host Value: localho ...
- JAVA ——int 类型除法保留两位小数
@Test public void txfloat() { // TODO 自动生成的方法存根 int a=9; int b=7; DecimalFormat df=new DecimalFormat ...
- 洛谷 3112 [USACO14DEC]后卫马克Guard Mark——状压dp
题目:https://www.luogu.org/problemnew/show/P3112 状压dp.发现只需要记录当前状态的牛中剩余承重最小的值. #include<iostream> ...
- php收集表单数据-$GET和$POST的区别
学习笔记: $_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. 从带有 GET 方法的表单发送的信息(例如:http://www.r ...
- 层次分析法MATLAB
输入成对比较矩阵,输出权重值和一致性检验结果. disp('请输入判断矩阵A(n阶)'); A=input('A='); [n,n]=size(A); x=ones(n,100); y=ones(n, ...