[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 ...
随机推荐
- 实现js与Qt程序的交互(使用QtWebkit)
在QtWebkit的javascript里访问QObject的最关键的关键就是下面这个方法: void QWebFrame::addToJavaScriptWindowObject ( const Q ...
- c# 停靠窗体
public partial class FrmAnchor : Form, IMessageFilter { public FrmAnchor(Control parentControlc, Con ...
- 配置我的Ubuntu Server记(包括桌面及VNC,SSH,NTP,NFS服务) good
跟老板申请买了一台配置相对较好的计算机回来做GPU计算,当然,不能独享,所以做成服务器让大家都来用. 这篇日志用来记录配置过程中遇到的一些问题,以方便下次不需要到处谷歌度娘. 安装Server版系统 ...
- linux环境下使用百度云网盘
linux下经常需要备份一些文件到云端,现在能用的也就只有度娘的百度云网盘了,在github上发现一个挺好的项目,bypy,用来在linux下使用百度云. 项目地址:https://github.co ...
- Python自动化测试 (八)unittest 单元测试
任何一种编程语言, 都会有单元测试框架, 本文介绍Python 自带的unittest模块 # -* - coding: UTF- -* - class Myclass: def sum(self,x ...
- Spring Cloud Ribbon配置详解
概述 有时候需要自定义Ribbon的配置和客户端超时配置. 自动化配置 /* 使用属性自定义功能区客户端 从版本1.2.0开始,Spring Cloud Netflix现在支持使用属性与Ribbon文 ...
- java.nio.Buffer源码阅读
Java 自从 JDK1.4 起,对各种 I/O 操作使用了 Buffer 和 Channel 技术.这种更接近于操作系统的的底层操作使得 I/O 操作速度得到大幅度提升,下面引用一段<Java ...
- 【静态NAT】 为什么子网可以ping父网,但是父网ping不通子网?
为什么子网可以ping父网,但是父网ping不通子网? 这就好比在公网中ping一个192.168.0.x的子网,roter无法找到这个子网的地址,所以会把package丢掉. 如何解决呢,可以在路由 ...
- Java学习笔记-spring整合mybatis
这个项目就是一个例子,只有添加图书的功能: 项目架构: resource: 整合流程: 1.pom文件节点,这两个是整合用的,其他节点不再赘述: <!-- https://mvnreposito ...
- CentOS 7更新系统时间
Linux系统在安装的时候,总是会出现时区,时间的错误. 将Linux系统时间和本地区网络时间同步,ntpdate可以从网络同步时间, 需要安装sudo yum install ntp ntpdate ...