[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 ...
随机推荐
- 使用 Visual Studio 开发并调试 Mail Add-in (mail app for Outlook)
准备工作 如果你的邮箱搭建在 Exchange Server 上,则可以创建邮件应用程序(Mail Add-in)来扩展Office本身的功能,使用 Office Add-in Model 开发的 M ...
- qobject_cast<QPushButton*>(sender()) 简化信号与槽的编写(sender()取得发信号的对象后,就取得了它的全部信息,为所欲为)
当你觉得写代码是一件重复性极高的工作时,这时你就应该考虑换个方式来实现了. 提高代码效率,减少代码量. 代码片: void Widget::onClicked() { QPushButton* but ...
- Windows10 下运行Linux子系统
关于Windows10 下运行Linux子系统: Windows10内置Linux子系统初体验:http://www.jianshu.com/p/bc38ed12da1d Win10运行Ubuntu版 ...
- Python连载10-os包函数(续)
一.os包(接连载9) 1.函数:system() (1)用法:运行系统shell命令 (2)格式:os.system(系统命令) (3)返回值:打开一个shell或终端界面 (4)注意:一般是用su ...
- asp.net core 系列之Response caching 之 Distributed caching(3)
这篇文章讲解分布式缓存,即 Distributed caching in ASP.NET Core Distributed caching in ASP.NET Core 分布式缓存是可以在多个应用服 ...
- vue结合Ant Design实现后台系统的权限分配(支持无限子级嵌套)
最近公司的业务需要,要做一个后台管理系统的管理系统类似于这样子 功能需求如下: 左边是权限菜单,右边对应的是具体权限. 1.父级权限菜单选中,父级权限菜单的权限包括其中所有子级权限菜单的权限也要选中, ...
- IM推送保障及网络优化详解(一):如何实现不影响用户体验的后台保活
对于移动APP来说,IM功能正变得越来越重要,它能够创建起人与人之间的连接.社交类产品中,用户与用户之间的沟通可以产生出更好的用户粘性. 在复杂的 Android 生态环境下,多种因素都会造成消息推送 ...
- Azkaban学习之路(三)—— Azkaban Flow 1.0 的使用
一.简介 Azkaban主要通过界面上传配置文件来进行任务的调度.它有两个重要的概念: Job: 你需要执行的调度任务: Flow:一个获取多个Job及它们之间的依赖关系所组成的图表叫做Flow. 目 ...
- Salesforce Admin篇(一)Duplicate Management
参考资料:https://help.salesforce.com/articleView?id=managing_duplicates_overview.htm Salesforce 很重要的一个平台 ...
- 关于火狐浏览器设置cookie的一个问题
最近发现我一个项目的网页,里面的cookie无法添加了,急的我瞪着我的PHP代码沉思了好久,我默认用的火狐浏览器,然而我默默的打开另一个叫360的浏览器,发现它的cookie是正常添加的. ... 难 ...