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 ...
随机推荐
- Android,不小心关闭了某个小窗口怎么恢复,方法介绍
Window > Show View > Other 需要哪个窗口就用哪个~
- 神经网络和Deep Learning
参考资料: 在线免费书籍 http://neuralnetworksanddeeplearning.com/chap1.html Chapter 1 1. perceptron 感知机 it's a ...
- BZOJ 3781: 小B的询问
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 643 Solved: 435[Submit][Status][Discuss ...
- java单例的几种实现方法
java单例的几种实现方法: 方式1: public class Something { private Something() {} private static class LazyHolder ...
- sql 的实用函数(包含日期函数、截取字符串函数)
CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(data_type(length),data_to_b ...
- python:mysql+pycharm+Django环境搭建
1.安装mysql-python 环境:OS X Yosemite10.10.2 + Python2.7 首先网上搜了下mysql-python,说要先安装mysql客户端,然后改配置文件,可是各种改 ...
- 程序代码中退出函数exit()与返回函数return ()的区别
程序代码中退出函数exit()与返回函数return ()的区别 exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数 ...
- Zabbix3 agent端安装(二)
1.基础环境准备 安装zabbix的yum源,这里有必要提一点,阿里的yum源已经提供了zabbix3.0 1.1.yum源配置 rpm -ihv http://mirrors.aliyun.com/ ...
- ThinkPhP 分页
<?php namespace User\Service; use Think\Controller; use Think\Exception; class UserService { // 每 ...
- PHP中判断变量为空的几种方法
判断变量为空,在许多场合都会用到,同时自己和许多新手一样也经常会犯一些错误, 所以自己整理了一下PHP中一些常用的.判断变量为空的方法. 1. isset功能:判断变量是否被初始化本函数用来测试变量是 ...