Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1
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.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root) {
if(root==NULL) return true;
int l=height(root->left);
int r=height(root->right);
if(l<||r<||l-r>||r-l>) return false;
return true;
}
int height(TreeNode *root){
if(root==NULL) return ;
int l=height(root->left);
int r=height(root->right);
if(l<||r<||l-r>||r-l>) return -;
return max(l,r)+;
}
};
其他做法:
转自:http://blog.csdn.net/feliciafay/article/details/18348065
// 68ms过大集合 简洁版
public:
int cntHeight(TreeNode *root) {
if(root == NULL) return ;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return -; //自定义 return -1,表示不平衡的情况
else return max(l, r) + ;
}
bool isBalanced(TreeNode *root) {
if(root == NULL) return true;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return false;
else return true;
}
// 68ms过大集合 更简洁版
public:
int cntHeight(TreeNode *root) {
if(root == NULL) return ;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return -; //自定义 return -1,表示不平衡的情况
else return max(l, r) + ;
}
bool isBalanced(TreeNode *root) {
if(root == NULL) return true;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return false;
else return true;
}
Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1的更多相关文章
- 【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] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- linux随笔二
1.查看整个文件 cat mongo.sh 查看脚本文件的内容:mongo 172.60.0.203:27017/che001 -uplatform -pplatform cat -n **,查 ...
- 利用bochs调试Linux 0.11内核
引导程序调试软件bochs,跟配套的linux0.11内核img下载地址分别是: http://sourceforge.net/projects/bochs/http://www.oldlinux.o ...
- 九度oj 题目1376:最近零子序列
题目描述: 给定一个整数序列,你会求最大子串和吗?几乎所有的数据结构与算法都会描述求最大子串和的算法.今天让大家来算算最近0子串和,即整数序列中最接近0的连续子串和.例如,整数序列6, -4, 5, ...
- javascript图片放大镜效果展示
javascript图片放大镜效果展示 <!DOCTYPE html> <html> <head lang="en"> <meta cha ...
- oracle查询包含大小写的数据
查询包含小写的所有数据: select oper_no from info_oper where regexp_like(oper_no,'[[:lower:]]'); select oper_no ...
- 利用Python从文件中读取字符串(解决乱码问题)
首先声明这篇学习记录是基于python3的. python3中,py文件中默认的文件编码就是unicode,不用像python2中那样加u,比如u'中文'. 不过在涉及路径时,比如C:\Users\A ...
- kb-07-RMQ线段树--07(动态规划)
RMQ是一类解决区间最值查询的算法的通称:.一共有四类:在代码中有说明: 下面是ST算法,就是动态规划做法: 来看一下ST算法是怎么实现的(以最大值为例): 首先是预处理,用一个DP解决.设a是要求区 ...
- [LOJ#516]「LibreOJ β Round #2」DP 一般看规律
[LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...
- BZOJ 4554 [Tjoi2016&Heoi2016]游戏 ——二分图
出原题,直接二分图匹配即可. #include <cstdio> #include <vector> #include <cstring> using namesp ...
- JavaWeb学习总结(十七)——JSP中的九个内置对象(转)
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...