【一天一道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 ...
随机推荐
- Java8-理解Colloctor
上一节学习了Java8中比较常用的内置collector的用法.接下来就来理解下collector的组成. Collector定义 Collector接口包含了一系列方法,为实现具体的归约操作(即收集 ...
- MySQL my.cnf 配置文件注释
以下是my.cnf配置文件参数解释 [client] port = 3309socket = /home/longxiben ...
- jquery常用函数
.text() //获得或更改元素文本: .html() //获得或更改元素标签: .val() //获得或更改input值: .css() //获得或更改元素样式: .click() //点击触发事 ...
- 【python进阶】Garbage collection垃圾回收2
前言 在上一篇文章[python进阶]Garbage collection垃圾回收1,我们讲述了Garbage collection(GC垃圾回收),画说Ruby与Python垃圾回收,Python中 ...
- Java8 按照类属性去重
测试po package com.shiwulian.test.po; public class Person {private String id;private String name;priva ...
- C实战:项目构建Make,Automake,CMake
C实战:项目构建Make,Automake,CMake 在本系列文章<C实战:强大的程序调试工具GDB>中我们简要学习了流行的调试工具GDB的使用方法.本文继续"C实战" ...
- Android图表库MPAndroidChart(九)——神神秘秘的散点图
Android图表库MPAndroidChart(九)--神神秘秘的散点图 今天所的散点图可能用的人不多,但是也算是图表界的一股清流,我们来看下实际的效果 添加的数据有点少,但是足以表示散点图了,我们 ...
- 深度学习&机器学习资源汇总1
本篇博客的目地,是对工作学习过程中所遇所见的一些有关深度学习.机器学习的优质资源,作分类汇总,方便自己查阅,也方便他人学习借用. 主要会涉及一些优质的理论书籍和论文.一些实惠好用的工具库和开源库.一些 ...
- 采购订单状态更改处理API
--PO采购订单状态更改处理API PO_Document_Control_PUB.control_document( p_api_version IN NUMBER, p_init_msg_list ...
- Xcode8出现的一些常见问题
消除无用输出语句问题:Xcode8之后,新创建的项目在手机上运行后,就会在输出窗口,输出一大堆乱七八糟的日志,对我们几乎没有用,如图: 解决办法: [product]-[scheme]-[Edit S ...