【一天一道LeetCode】#110. Balanced Binary Tree
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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。
可以采用递归的思想,判断一个二叉树是不是平衡二叉树,需要判断它的左右子树是不是平衡二叉树,依次递归下去。
也就是说,把二叉树的每一个节点都当做根节点,判断它的左右子树是否平衡。
那么,当前节点的左右子树的高度,应该等于左右子树中较高的那个子树的高度加上1。
当根节点为NULL的时候直接返回0.
具体解释见代码:
/**
* 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 dfsTree(root)!=-1;
}
int dfsTree(TreeNode* root)
{
if(root==NULL) return 0;//空节点返回0
int left = dfsTree(root->left);//求左子树的高度
if(left == -1) return -1;//-1代表不平衡
int right = dfsTree(root->right);//求右子树的高度
if(right== -1) return -1;//-1代表不平衡
if(abs(left-right)>1) return -1;///如果不平衡则返回-1
return max(left,right)+1;//否则返回较高的那一个加上1
}
};
【一天一道LeetCode】#110. Balanced Binary Tree的更多相关文章
- 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 ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
随机推荐
- jquery easyui datagrid 编辑行 类型combobox
完成编辑瞬间datagrid中显示的是combobox的value 而非text var rows=$('#tb1').datagrid('getRows'); for(var i=0;i<ro ...
- easyui combobox setValue数据绑不上
var synj = "<%=arrbj[3]%>"; var xnxq = "<%=xnxq%>"; OnchangeSelect($ ...
- 如果将Joomla网站搜索结果显示到一个“干净”页面
有时候大家会发现Joomla网站自带的或者第三方的搜索功能时,搜索结果会显示在首页,和首页其它的模块如图片橱窗等显示在一起,非常混乱. 在这里教大家一个不需要修改代码的小技巧来解决这个问题,使搜索结果 ...
- nodeppt的使用教程
为什么选择nodeppt 这可能是迄今为止最好的网页版演示库 基于GFM的markdown语法编写 支持html混排,再复杂的demo也可以做! 支持多个皮肤:colors-moon-blue-dar ...
- Python中如何自定义一个计时器
import time as t class MyTimer(): # 初始化构造函数 def __init__(self): self.prompt = "未开始计时..." s ...
- POJ 放苹果问题(递归)
首先我们想象有一个函数count f(m,n)可以把m个苹果放到n个盘子中. 根据 n 和 m 的关系可以进一步分析: 特殊的m <=1|| n <= 1时只有一种方法: 当 m < ...
- Node.js ZLIB
Zlib 稳定性: 3 - 文档 可以通过以下方式访问这个模块: var zlib = require('zlib'); 这个模块提供了对 Gzip/Gunzip, Deflate/Inflate, ...
- Python3 条件控制
if 语句 Python中if语句的一般形式如下所示: if condition_1: statement_block_1 elif condition_2: statement_block_2 el ...
- OC基础之推荐一个旋转木马(跑马灯)效果的图片展示Demo
这个旋转木马(跑马灯)效果的图片展示Demo,包括设定旋转方向,图片倒影,背景设置,旋转速度,开始结束,点击显示选中的图片,彩色的块展示等等功能 效果图:(源码下载:https://github.co ...
- Android ocr识别文字介绍(文字识别)
最近在做身份证号码识别,在网上搜索的一番后发现目前开源的OCR中tesseract-ocr算是比较强大的了,它由HP于1985年到1995年间开发,后来由google直接负责,经过谷歌进一步开发后,目 ...