LeetCode——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,但马上发现这是一个递归过程,每次递归返回的是深度,可是还得判断是否平衡,如果不平衡如何返回是否平衡,
然后你可能会想到使用两个函数,一个函数用于递归求深度,一个函数用于递归求是否平衡,如下:
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
int l=depth(root.left);
int r=depth(root.right);
return ((int)Math.abs(l-r)<2)&&isBalanced(root.left) && isBalanced(root.right);
}
static int depth(TreeNode n){
if(n==null) return 0;
return Math.max(depth(n.left),depth(n.right))+1;
}
再然后你会发现时间复杂度为O(n^2),做了很多的无用功。
要想降低时间复杂度,就得想一个办法让我们在递归求深度的同时判断是否是平衡二叉树,也就是还是得解决求深度的时候递归返回值的问题,在LeetCode中discuss了一下
,然后发现了大神们用了一个求深度时不可能出现的值轻松解决问题,关键代码如下:
public final int UNB = -99;
public int balanceJudge(TreeNode root){
if(root==null)return 0;
int l = balanceJudge(root.left);
int r = balanceJudge(root.right);
if(l==UNB || r== UNB || Math.abs(l-r)>1) return UNB;
return 1+(l>r?l:r);
}
最后只需要判定返回值否为UNB就可以知道改二叉树是否平衡了。。
完整代码如下(java):
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public final int UNB = -99;
public boolean isBalanced(TreeNode root) {
int result = balanceJudge(root);
if(result != UNB)return true;
else return false;
} public int balanceJudge(TreeNode root){
if(root==null)return 0;
int l = balanceJudge(root.left);
int r = balanceJudge(root.right);
if(l==UNB || r== UNB || Math.abs(l-r)>1) return UNB;
return 1+(l>r?l:r);
}
}
LeetCode——Balanced Binary Tree(判断是否平衡二叉树)的更多相关文章
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [Leetcode] Balanced binary tree平衡二叉树
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 ...
- balanced binary tree(判断是否是平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree 判断平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...
- LeetCode - Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
随机推荐
- AngularJS 的嵌套路由 UI-Router
AngularJS 的嵌套路由 UI-Router 本篇文章翻译自:https://scotch.io/tutorials/angular-routing-using-ui-router 演示网站请查 ...
- <<< sqlserver评估过期解决
点击开始-所有程序-Microsoft SQL Server 2008-配置工具-SQL Server 安装中心然后点击左侧的维护,在点击右侧的版本升级,接着按照提示一直点下一步,到产品密钥的时候输入 ...
- jquery实现简单瀑布流布局(续):图片懒加载
# jquery实现简单瀑布流布局(续):图片懒加载 这篇文章是jquery实现简单瀑布流布局思想的小小扩展.代码基于前作的代码继续完善. 图片懒加载就是符合某些条件时才触发图片的加载.最常见的具体表 ...
- 什么?你还不会写JQuery 插件
前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论 ...
- [转] 64位windows下添加postgreSQL odbc数据源
系统环境:windows7 64位 postgreSQL9.0(64bit) ps:安装postgreSQL时确定安装了odbc驱动. 问题:点击“开始->控制面板->管理工具-& ...
- [UML]UML系列——类图Class
相关文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend) 一.类图的概念及组成 1.类图的概念 类图是描述类.接 ...
- ffmpeg常用转换命令,支持WAV转AMR
音频转换: 1.转换amr到mp3: ffmpeg -i shenhuxi.amr amr2mp3.mp3 2.转换amr到wav: ffmpeg -acodec libamr_nb -i shenh ...
- [译]JavaScript源码转换:非破坏式与再生式
原文:http://ariya.ofilabs.com/2013/06/javascript-source-transformation-non-destructive-vs-regenerative ...
- Android网络请求通信之Volley
一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...
- TypePerf.exe使用命令查找计数器
TypePerf.exe是一个命令行工具,包括把Windows操作系统的性能计数器数据输出到命令窗口或写入到支持该功能的日志文件格式中. 通过以下两个参数获取计数器信息: -q [object] 列出 ...