【LeetCode】110. 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.
提示:
此题要求判断一个给定的二叉树是否是“平衡”的,这里“平衡”的定义是要求每个节点的左右两颗子树高度之差不能大于1,解题的思想是使用DFS。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
return dfs(root) != -;
}
int dfs(TreeNode* root) {
if (root == NULL) return ;
int left = dfs(root->left);
if (left == -) return -;
int right = dfs(root->right);
if (right == -) return -; if (abs(left - right) > ) return -;
return max(left, right) + ;
}
};
【LeetCode】110. Balanced Binary Tree的更多相关文章
- 【一天一道LeetCode】#110. Balanced Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 【leetcode❤python】110. Balanced Binary Tree
#-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):# def _ ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
随机推荐
- [笔记]我的Linux入门之路 - 03.Java环境搭建
其实ubuntu是自带一个叫openJDK的东西的,是谷歌看Oracle不爽而搞的.不过呢...总感觉不太习惯,况且我既然都来Linux了,总是想折腾一把的. 首先先检查下有没有安装java.终端输入 ...
- 1-LPC1778建立工程
先来建立一个工程模板,,,要比32简单的多,假设32是用库开发的话,,,,因为还要把那些和库相关的文件加到工程里.... LPC呢就只需要把设置系统和总线的时钟文件(system_LPC177x_8x ...
- 开涛spring3(2.3) - IoC的配置使用
2.3.1 XML配置的结构 一般配置文件结构如下: <beans> <import resource=”resource1.xml”/> <bean id=”bean ...
- C# 特性参数(注解属性加在参数前面)
特性参数 webapi 框架里有很多特性参数,为了解除一些新人的疑惑,写个小例子分享下. class Program { static void Main(string[] args) { var m ...
- 每R一点:各种画地图,全是知识点,90%人不知道!(转)
R语言绘制地图,在数据分析中经常能够用到,并且会达到非常好的展示效果,本节以例子形式,介绍如何使用R语言工具,画出理想的地图. 本节例子在 R version 2.15.3版本下运行顺畅,其他版本待定 ...
- MPP 一、Greenplum 集群安装
Installating and Initializing a Greenplum Database System... 1 安装说明 1.1 环境说明 名称 版本 下载地址 虚拟机 Oracle V ...
- C++ STL快速入门
在数月之前的机试中第一次体验到STL的威力,因为自己本来一直在用C语言做开发,很多数据结构都是自己造的,比如链表.队列等,第一次接触C++ STL后发现这些数据结构都已经给我提供好了,我直接拿去调用就 ...
- Java经典编程题50道之四十一
海滩上有若干个一堆桃子,五只猴子来分.第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份. 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中, ...
- php注册登录源代码
php注册登录源代码 链接数据库<?php$conn=mysql_connect('localhost','root','');mysql_select_db('ht',$conn);mysql ...
- javaCV图像处理之Frame、Mat和IplImage三者相互转换(使用openCV进行Mat和IplImage转换)
前言:本篇文章依赖四个jar包,其中javacv.jar,javacpp.jar和opencv.jar为固定jar包,opencv-系统环境.jar为选配(根据自己的系统平台,x64还是x86而定) ...