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);
    }
}
时间: 2024-10-08 17:32:53

LeetCode——Balanced Binary Tree(判断是否平衡二叉树)的相关文章

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. //两次递归效率不高 合并看leetcode代码 /** * Defi

LeetCode Balanced Binary Tree (判断平衡树)

题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),

[leetcode]Balanced Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于1.这实际上是AVL树的定义.首先要写一个计算二叉树高度的函数,二叉树的高度定义为:树为空时,高度为0.然后递归求解:树的高度 = max(左子树高度,右子树高度)+1(根节点要算上).高度计算函数实现后,递归求解每个节点的左右子树的高度差,如果有大于1的

LeetCode: Balanced Binary Tree [110]

[题目] 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. [题意] 判断二叉树是否是平衡二叉树 [思路] 平衡二叉树要

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. 中文:给定一棵二叉树,检测其高度上平衡否. 于此题,一棵高度上平衡的二

[LeetCode] [Balanced Binary Tree 2012-10-08 ]

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. The key problem is that recursive f

LeetCode: Balanced Binary Tree 解题报告

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. Show Tags SOLU

[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,为平衡二叉

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. Hide Tags Tree Depth-first Search /