[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 ...
随机推荐
- 使用EurekaLog时遇到的问题
1.在DLL项目中千万不要加入EurekaLog,不然在主程序调用时就会出现莫名其妙的内存问题. 2.要使用EurekaLog发邮件的功能,发邮件的SMTP服务器必须支持8bit MIME编码.如SI ...
- 一条命令,秒秒钟完成MD5、SHA1校验,这就叫效率!
相信很多奋斗在运维战线的小伙伴们经常会遇到版本升级之类的问题.笔者之前所在的公司每次进行版本发布的时候都会附带MD5校验哈希值,每次升级之前一般都要核对MD5哈希值的,刚刚开始的时候对Linux并不是 ...
- qt 心跳设计
网络通信中的心跳设计是为了判断客户端和服务器通信是socket是否处于连接状态,服务端每隔一个固定的时间间隔给客户端放消息,客户端设计一个心跳类,类中有一个定时器,当socket接收到信息时,心跳类记 ...
- Bootstrap3.0学习(一)
Bootstrap是Twitter退出的一个开源的用于前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架.Bootstra ...
- Spring_two
Spring_two 基于注解的IOC配置 准备工作(参考上一篇) ); 接口的实现类AccountDaoImpl.java修改 /** * 账户的持久层实现类 */ @Repository(&quo ...
- Java NIO 学习笔记(六)----异步文件通道 AsynchronousFileChannel
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
- Java NIO 学习笔记(五)----路径、文件和管道 Path/Files/Pipe
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
- 继承Comparable接口来实现排序
1.java代码里进行排序(若sql能排序,就不要用代码排序) 可以继承Comparable接口来实现,若是在类中,可以声明Comparator对象,来进行比较 List<Map> map ...
- MyBatis无限级分类实现的两种方法--自关联与map集合
1.这回先创建数据库吧 下表cid是CategoryId的缩写,cname是CategoryName的缩写,pid是parentId的缩写 无限级分类一般都包含这三个属性,至少也要包含cid和pid才 ...
- canvas 画布基本操作
const canvas = document.getElementById('canvas'); // 2.画笔 --- canvas的上下文对象 const ctx = canvas.getCon ...