110. Balanced Binary Tree
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.
可以先求出每个subtree的深度,然后比较。但不高效,时间复杂度为O(nlgn).因为每一次计算左右subtree深度为2*O(lgn), 然后递归n次。
public bool IsBalanced(TreeNode root) {if(root == null) return true;int left = TreeHeight(root.left);int right = TreeHeight(root.right);if ((left>=right+2)||(left+2<=right)) return false;return IsBalanced(root.left)&& IsBalanced(root.right);}public int TreeHeight(TreeNode root){if(root == null) return 0;if(root.left == null && root.right == null) return 1;return Math.Max(TreeHeight(root.left), TreeHeight(root.right))+1;}
优化的方法为DFS,先check更小的subtree是否为Balanced Tree, 如果不是则直接返回false。参考http://www.cnblogs.com/grandyang/p/4045660.html
public bool IsBalanced(TreeNode root) {return !(TreeHeight(root)==-1);}public int TreeHeight(TreeNode root){if(root == null) return 0;int left = TreeHeight(root.left);if(left == -1) return -1;int right = TreeHeight(root.right);if(right == -1) return -1;if ((left>=right+2)||(left+2<=right)) return -1;return Math.Max(left, right)+1;}
转载于:https://www.cnblogs.com/renyualbert/p/5863170.html
110. Balanced Binary Tree相关推荐
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- leetCode 110. Balanced Binary Tree 平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...
- leetcode python3 简单题110. Balanced Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...
- 110. Balanced Binary Tree 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...
- 110.Balanced Binary Tree
题目描述(简单难度) 判断一棵树是否是平衡二叉树,平衡二叉树定义如下: 它是一棵空树或它的左右两个子树的高度差的绝对值不超过 1,并且左右两个子树都是一棵平衡二叉树. 解法一 直接按照定义来吧,并且多 ...
最新文章
- 端口基础常识大全+常用端口对照
- 嵌入式linux dhcp移植,嵌入式linux中的dhcp服务器
- MyEclipse扩展功能设置(Eclipse代码提示功能)
- win10 自定义分辨率
- Live555研究之二Sleep实现
- Android BitmapShader 实战 实现圆形、圆角图片
- MySQL 删除数据
- Unity3D 渲染统计窗口
- 【Kafka】Kafka inter.broker.listener.name must be a listener name defined in advertised.listeners
- click Documenting Scripts
- SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法...
- SpringMVC路径配置
- 2步轻松搞定SpringBoot2.x分布式session共享,极少配置
- PDA程序 点击登录按钮显示提示信息
- mysql数据库升幂_斯特林数
- 如何在没有电脑的情#况下用安卓手机制作windows pe启动盘
- 代码 马佳义_武汉大学电子信息学院
- 贾俊平统计学思维导图- 第一章 导论
- 石家庄地铁查询(双人项目)
- PHR-search:一个基于预测蛋白质层次关系的蛋白质远程同源性检测搜索框
热门文章
- 星际战争2服务器维护,星际战争2初次反击虚空技能洗练技巧
- mysql的本地id可以随便设置马_MySQL中的账号与权限管理
- 安装sqlserver时“试图执行未经授权的操作“的错误
- IIS下配置php运行环境
- HTML5+NodeJs实现WebSocket即时通讯
- batch-size 深度学习笔记
- Nutch+Hadoop集群搭建
- Cert manager自动签发/更新证书
- 10个节省时间和改善工作流的Git技巧
- 各大公司广泛使用的在线学习算法FTRL详解