【一天一道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 ...
随机推荐
- Tinychain 是比特币的一个简易口袋实现
Putting the rough in "rough consensus" Tinychain is a pocket-sized implementation of Bitco ...
- Java 中 json字符串转换为类
使用到alibaba.fastjson包 具体实现 JSONObject jsonObject = JSONObject.parseObject(msg); SmsSenderStatus smsSe ...
- Cisco banner 登陆消息提示设置命令详解
从法律角度来看,登陆消息非常重要.当入侵者进入网络而没有受到适当的警告时,他们有可能赢得官司.在放置登陆消息之前应让律师检查下,永远不要使用"欢迎"等问候语,以免被误解为邀请大家使 ...
- 9.QT-标准对话框
Qt提供的可复用的标准对话框,全部继承自QDialog类,如下图所示: QMessageBox:信息对话框,用于显示信息.询问问题等: QFileDialog:文件对话框 QColorDialog:颜 ...
- Filter,FilterChain,FilterConfig
实例: package com.zillion.app.filter; import java.io.IOException; import javax.servlet.Filter; import ...
- css控制file控件透明 漂浮
css控件透明属性设置IE firefor设置方法<STYLE type=text/css>.upfilefield{position:absolute; FILTER: alpha(op ...
- 关于一些基础的Java问题的解答(三)
11. HashMap和ConcurrentHashMap的区别 从JDK1.2起,就有了HashMap,正如上一个问题所提到的,HashMap与HashTable不同,不是线程安全的,因此多线程 ...
- javascrpt_数组学习
1.构造函数 var arr = new Array(); Array 构造函数有一个很大的缺陷,就是不同的参数,会导致行为不一致. 因此,不建议使用它生成新数组,直接使用字面量是最好的做法. 2.静 ...
- ERP中的地区管理
地区管理 地区管理主要实现地区数据的添加.编辑.查看.启用.禁用等功能,另外还包含地区选择控件封装. 业务功能点: 地区数据查看:地区列表树状展现,列表增加省.市.区.县.乡图标. 地区选择控件:选择 ...
- OpenCv error :unresolved external symbol(链接库没有加上)
Error 如下:Linking...: error LNK2001: unresolved external symbol _cvDestroyWindow: error LNK2001: unre ...