[leetcode] 110. Balanced Binary Tree (easy)
原题链接
水题
深度搜索每一节点的左右深度,左右深度差大于1就返回false。
class Solution {
public:
bool isBalanced(TreeNode *root) {
bool flag = true;
if (!root) return true;
dfs(root, flag);
return flag;
}
private:
int dfs(TreeNode *root, bool &flag) {
if (!root) return 0;
int leftH = dfs(root->left, flag) + 1;
int rightH = dfs(root->right, flag) + 1;
if (abs(leftH - rightH) > 1) flag = false;
return max(leftH, rightH);
}
};
[leetcode] 110. Balanced Binary Tree (easy)的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java [Leetcode 110]Balanced Binary Tree
题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- [LeetCode] 110. Balanced Binary Tree 解题思路
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 浅析C#代理
delegate 是委托声明的基础,是.net 的委托的声明的关键字action 是基于delegate实现的代理 有多个参数(无限制个数)无返回值的代理 func 是基于delegate实现的代理 ...
- MongoDB自学日记2——权限
首先应该明确的是为什么要学MongoDB.OK,如果是仅仅出于对于流行技术的原始兴趣,可能并不能深入学习,还必须有应用需求.刚开始学习MongoDB,因为以前对其它数据库的了解也不是特别深入,所以许多 ...
- Spring之基于注解的注入
对于DI使用注解,将不再需要在Spring配置文件中声明Bean实例.Spring中使用注解,需要在原有Spring运行环境基础上再做一些改变,完成以下三个步骤. (1)导入AOP的Jar包.因为注解 ...
- Exceptionless(二) - 使用进阶
Exceptionless(二) - 使用进阶 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/11100563.html 官网 ...
- Scala 学习之路(九)—— 继承和特质
一.继承 1.1 Scala中的继承结构 Scala中继承关系如下图: Any是整个继承关系的根节点: AnyRef包含Scala Classes和Java Classes,等价于Java中的java ...
- MapReduce in MongoDB
MongoDB の MapReduce 在Hadoop的学习中已经接触过MapReduce了,它是一个很成熟的计算模型,将大批量的工作(也就是数据)分解(MAP映射)执行,最后将结果合并成最终的Red ...
- 巧妙解决element-ui下拉框选项过多的问题
1. 场景描述 不知道你有没有这样的经历,下拉框的选项很多,上万个选项甚至更多,这个时候如果全部把数据放到下拉框中渲染出来,浏览器会卡死,体验会特别不好 用人会说element-ui的select有一 ...
- spring源码深度解析— IOC 之 循环依赖处理
什么是循环依赖 循环依赖其实就是循环引用,也就是两个或则两个以上的bean互相持有对方,最终形成闭环.比如A依赖于B,B依赖于C,C又依赖于A.如下图所示: 注意,这里不是函数的循环调用,是对象的相互 ...
- python学习 -女神或者男神把微信消息撤回后好慌,有了这个妈妈再也不担心你看不到女神或者男神撤回的消息了(超详解)
简介 有时候在忙工作,女朋友发了一个消息,就撤回了,但是人天生的都有一颗好奇心,而且在当今这个时代找个女朋友不容易,一个程序猿找一个女朋友更是不容易的.人家好不容易跟你,你还不得把人家当老佛爷侍候着, ...
- BZOJ 1483:[HNOI2009]梦幻布丁(链表启发式合并)
http://www.lydsy.com/JudgeOnline/problem.php?id=1483 题意:中文. 思路:对于每一种颜色,用一个链表串起来,一开始保存一个答案,后面颜色替换的时候再 ...