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 ...
随机推荐
- 代码重构-2 简单不变的 if else 用字典代替
原代码 private string GetExDesc(string lotteryCode) { string exDesc = "抽奖"; if (lotteryCode.T ...
- TEXT宏,TCHAR类型
TCHAR *ptch = TEXT("This is a const string."); 如果使用UNICODE字符集, 则TEXT("This is a const ...
- eq相等 ne、neq不相等, gt大于, lt小于 gte、ge大于等于 lte、le 小于等于 not非 mod求模 等
eq相等 ne.neq不相等, gt大于, lt小于 gte.ge大于等于 lte.le 小于等于 not非 mod求模 is [not] div by是否能被某数整除 i ...
- linux 下安装开发组件包
最初安装redhat 时, 系统自己装的,只安装了base 包,在开发过程中,需要不停的安装某个需求包, 图省事,安装光盘下的开发组件包: 在安装光盘下,,,用命令: yum grouplist ...
- DOS中如何删除文件夹
可以使用rd命令.如果目录是空的,那么可以用 rd 目录删除目录.如下图:输入Y之后系统就会删除整个目录. 如果目录是非空,那么使用rd 目录 /s来删除目录(目录下的文件也会删除一同).如果没有加/ ...
- Asp.Net之后台加载JS和CSS
在Asp.Net开发时,用到的JS库.通用的CSS等,在许多页面都会用到,而每次都需要手动引入,相当麻烦,而且有时一旦忘了引用,还得找半天才能找到问题.那有没有什么办法能够一劳永逸的呢?答案是有的. ...
- 深入浅出JSON
JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成.它基于ECMA262语言规范(1999 ...
- Win7下判断当前进程是否以管理员身份运行
判断当前程序是否以管理员身份运行,代码如下: #include <iostream> #include <windows.h> using namespace std; // ...
- 鸟哥的linux私房菜学习笔记 __ 命令与文件的搜寻
连续输入两次[tab]按键就能够知道使用者有多少命令可以下达.那你知不知道这些命令的完整档名放在哪里?举例来说,ls 这个常用的命令放在哪里呢? 就透过 which 或 type 来找寻吧! 范例一: ...
- Python 脚本之获取CPU信息
#!/usr/bin/env Python from __future__ import print_function from collections import OrderedDict impo ...