【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡
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.
以下解法为什么时间复杂度为O(n)?
/**
* 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:
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); //-1表示不平衡
int r = cntHeight(root->right); //-1表示不平衡
if(l < || r < || abs(l-r) > ) return false;
else return true;
}
};
【easy】110. Balanced Binary Tree判断二叉树是否平衡的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 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 解题思路
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 记一次生产数据库"意外"重启的经历
前言 在一个阳光明媚的下午,电脑右下角传来一片片邮件提醒,同时伴随着微信钉钉的震动,打开一看,应用各种出错,天兔告警,数据库服务器内存爆红,Mysql数据库实例挂掉了. 排查 先交代一下数据库版本: ...
- Practical Mathematical Handwriting
In this article, I discuss the handwriting of $\mathbb{A}, \mathcal{A}, \mathscr{A}, \mathfrak{A}$'s ...
- 封装的通过微信JS-SDK实现自定义分享到朋友圈或者朋友的ES6类!
引言: 我们经常在做微信H5的过程中需要自定义分享网页,这个如何实现呢?请看如下的封装的ES6类及使用说明! /** * @jssdk js对象,包括appId,timestamp,nonceStr, ...
- echarts报表显示%+没有0
function showTablegroup(page) { var series; $.ajax({ type:'post', url:"<%=basePath%>flowA ...
- HDU 3518 Boring counting
题目:Boring counting 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 题意:给一个字符串,问有多少子串出现过两次以上,重叠不能算两次 ...
- 第五章· Redis主从复制介绍
一.Redis主从复制 二.Redis主从复制工作机制 一.Redis主从复制 Redis复制功能简单介绍 1)使用异步复制.2)一个主服务器可以有多个从服务器.3)从服务器也可以有自己的从服务器.4 ...
- Python基础知识4--数据结构(树)
树 树的概念 堂兄弟的双亲不一定是兄弟关系. 二叉树 斜树 满二叉树 完全二叉树 二叉树的性质
- Linux(Ubuntu)使用日记------tenserflow安装(pip安装法)
其实步骤是很简单的,只是一开始在网上找了一份错误的教程的原因,掉入了坑. 安装过程: 1. 检查pip版本 pip3 -V 要求使用最新版的pip 9.0.1,如果不是,按照下面的方法安装最新的pip ...
- SSH 协议的 ssh StrictHostKeyChecking
项目的SFTP用到了这个参数: @Override public PooledObject<ChannelSftp> makeObject() throws Exception { JSc ...
- Jenkins与sonar扫描的集成问题
记录本周遇到的头疼了很久的一个问题,由于公司需要使用jenkins来自动管理构建项目,然后在关联sonar对项目代码质量进行审核. 接着坑爹的问题来了,原有的技术手段为项目构建成功后通过jenkins ...