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= ...
随机推荐
- 各种语言web性能简单对比测试
忽然想比较一下 python nodejs go 的web 响应,就简单的写了个性能对比测试. 测试目标:1 . i5 4核 32G 同一机器 linux 2. 用python(flas ...
- spring boot resttemplate发送post,get请求
参考博客: https://blog.csdn.net/weixin_42456466/article/details/80728387 https://www.cnblogs.com/QQParad ...
- IO流10 --- 缓冲流(字节型)实现非文本文件的复制 --- 技术搬运工(尚硅谷)
字节型缓冲流,BufferedOutputStream默认缓冲区大小 8192字节byte,满了自动flush() @Test public void test6(){ File srcFile = ...
- IO流4 --- IO流体系 --- 技术搬运工(尚硅谷)
- java-文件切割合并_对象的序列化
一 文件的操作 1.1 概况 1,切割文件的原理:一个源对应多个目的:切割文件的两种方式. 2,碎片文件的命名和编号. 3,程序代码体现. 4,如何记录源文件的类型以及碎片的个数(建立配置信息文件)( ...
- POJ4852 Ants
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20047 Accepted: 8330 Description ...
- Codeforces 1150D(字符串dp)
反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...
- Leetcode24.Swap Nodes in Pairs两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- Django 自定义auth_user
1 导入AbstractUser from django.contrib.auth.models import AbstractUser 1 2 创建类UserProfile并继承AbstractUs ...
- 洛谷 P3434 [POI2006]KRA-The Disks 贪心
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输出样例 输出样例 说明 思路 AC代码 题面 题目链接 P3434 [POI2006]KRA-The Disks 题目 ...