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.
在求高度的过程中判断是否为平衡树。。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
boolean flag=true;
public boolean isBalanced(TreeNode root) {
Depth(root);
return flag;
} //求高度,在此过程中判断是否平衡。
public int Depth(TreeNode node){
if(node==null) return 0;
int left=Depth(node.left);
int right=Depth(node.right);
if(Math.abs(left-right)>1)
flag=false;
return Math.max(left,right)+1;
}
}
balanced binary tree(判断是否是平衡二叉树)的更多相关文章
- Balanced Binary Tree 判断平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【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 Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- 平衡二叉树(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 ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
随机推荐
- android之View绘制
Android系统的视图结构的设计也采用了组合模式,即View作为所有图形的基类,Viewgroup对View继承扩展为视图容器类,由此就得到了视图部分的基本结构--树形结构 View定义了绘图的基本 ...
- UNIX网络编程——UDP编程模型
使用UDP编写的一些常见得应用程序有:DNS(域名系统),NFS(网络文件系统)和SNMP(简单网络管理协议). 客户不与服务器建立连接,而是只管使用sendto函数给服务器发送数据报,其中必须指定目 ...
- Mybatis执行CachingExecutor(六)
前面几篇博客我们介绍了Excutor及抽象类BaseExecutor和实现类SimpleExecutor.BatchExecutor和ReuseExecutor: 博客列表: Mybatis执行Exe ...
- 盘点:2016中国百强地产CIO高峰论坛的8大看点
2016年中国百强地产CIO高峰论坛将于2016年6月16日至18日在浙江湖州举行,届时百余位地产公司CIO将出席大会,共同探讨新形势下如何重塑IT价值,增强地产公司的市场竞争力和盈利能力. 此次大会 ...
- XML解析之SAX解析过程代码详解
上一篇谢了解析原理和过程,这里应用代码直观认识这个原理: 新建Demo1类: import java.io.File; import javax.xml.parsers.SAXParser; impo ...
- java设计模式---职责链模式
职责链的本质:分离职责,动态组合 样例: /** * 定义职责对象的接口 * */ public abstract class Handler { protected Handler successo ...
- Java中函数的递归调用
说到递归,java中的递归和C语言中也是很相似的,在Java中,递归其实就是利用了栈的先进后出的机制来描述的. public class HelloWorld { public static void ...
- Chapter 2 User Authentication, Authorization, and Security(3):保护服务器避免暴力攻击
原文出处:http://blog.csdn.net/dba_huangzj/article/details/38756693,专题目录:http://blog.csdn.net/dba_huangzj ...
- H5学习之旅-H5的新特性(1)
随着H5的发展,网页开发呈现了跨平台的特性,目前H5是兼容各大平台的,这也为一些其他的技术的跨平台开发提供了基础,所以我预测后H5会成为很重要的开发语言 新特性介绍 1.用于回话的canvas标签 2 ...
- shell,python获取当前路径(脚本的当前路径) (aso项目记录)
一.shell获取脚本当前路径 cur_dir=$(cd "$(dirname "$0")"; pwd) #获取当前脚本的绝对路径,参数$0是当前脚本对象 等 ...