LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树。
链接:https://oj.leetcode.com/problems/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.给出一棵二叉树,判断它是不是平衡二叉树。
平衡二叉树被定义每个节点的两个子树高度差不超过1。
这道题目比较简单,
一棵树是不是平衡二叉树,可以输入一个结点,计算它的两棵子树的高度差,然后与1比较,递归进行这个操作就可以完成判定。
回顾一下二叉树的概念,平衡二叉树基于二叉查找树(Binary Search Tree),
二叉查找树或者是一棵空树;
或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
二叉查找树是插入,排序,查找的平均时间复杂度都是O(Log(n)),最差情况是O(n),
二叉树的平衡因子是该结点的左子树的深度减去它的右子树的深度,平衡二叉树的BF因子绝对为1。
下面用Java完成Solution,注意空树的情况:
public class BalancedBinaryTreeSolution {
/**
* 访问局部内部类必须先有外部类对象,此处必须用Static修饰
*/
// public static class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int x){ val = x;}
// } //OJ支持JDK的Math函数
public boolean isBalanced(TreeNode root) {
//注意空树的情况
if(root == null)
return true; int leftDepth=getDepth(root.left);
int rightDepth=getDepth(root.right);
int altitude=rightDepth-leftDepth;
//注意只有一个顶点的情况
if(Math.abs(altitude)>1)
return false;
else
//递归比较
return isBalanced(root.left) && isBalanced(root.right);
} private int getDepth(TreeNode node){
if(node == null)
return 0;
//递归求深度
return 1+Math.max(getDepth(node.left), getDepth(node.right));
}
}
LeetCode之Balanced Binary Tree 平衡二叉树的更多相关文章
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [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 ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- Message
* Defines a message containing a description and arbitrary data object that can be* sent to a {@link ...
- ecshop后台导航修改教程说明
ecshop后台导航修改教程说明 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2014-06-25 需要操作的文件为: 1.修改admin\includes\in ...
- Oracle数据分页,并传出数据集
1.创建Package create or replace package forPaged is type my_csr is ref cursor; procedure getPaged(tabl ...
- 设定所有tableView中cell的分隔线颜色
上面只有针对xib或者storyboard中生成的tableview有效,如果想手码也有效,需在initwithframe中添加同样的方法
- H2Database数据类型
数据类型 整数(INT) 布尔型(BOOLEAN) 微整数(TINYINT) 小整数(SMALLINT) 大整数(BIGINT) 标识符(IDENTITY) 货币数(DECIMAL) 双精度实数( ...
- C++ 迭代器 基础介绍
C++ 迭代器 基础介绍 迭代器提供对一个容器中的对象的访问方法,并且定义了容器中对象的范围.迭代器就如同一个指针.事实上,C++的指针也是一种迭代器.但是,迭代器不仅仅是指针,因此你不能认为他们一定 ...
- <转>键盘扫描码
原文链接:http://www.cnblogs.com/wqw/archive/2009/08/30/1556618.html //以下是一个检测按键扫描码的程序 #i nclude <bios ...
- thinkphp中page方法
page方法也是模型的连贯操作方法之一,是完全为分页查询而诞生的一个人性化操作方法. 用法 我们在前面已经了解了关于limit方法用于分页查询的情况,而page方法则是更人性化的进行分页查询的方法,例 ...
- Java中的构造函数和重载
一.Java中的构造函数 构造函数是对象被创建时初始化对象的成员方法,它具有和它所在的类完全一样的名字.构造函数只能有入口参数,没有返回类型,因为一个类的构造方法的返回类就是类本身.构造函数定义后,创 ...
- bmob
移动后台: bmob http://baike.baidu.com/link?url=GHdwJY3cGygcfQDdzosckQnhVy1pvIGZA2Ws0K26lSSFGu7QRX4R1wlo6 ...