[LeetCode][Java] 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.

题意:

给定一棵二叉树,判定它是否为平衡二叉树。

算法分析:

* 平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),

* 且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,

* 并且左右两个子树都是一棵平衡二叉树。

* 下面的代码就完全按照定义,首先得到节点左右子树的高度(递归),然后判断左右子树是否为平衡二叉树,

*  利用递归完成整棵树的判断。完全满足条件,就返回true.

AC代码:

public class Solution
{
    //递归
    public boolean isBalanced(TreeNode root)
    {
        if(root==null) return true;
        int leftdepth;
        int rightdepth;
        leftdepth=maxDepth(root.left);
        rightdepth=maxDepth(root.right);
        if(Math.abs(leftdepth-rightdepth)<=1&&isBalanced(root.left)&&isBalanced(root.right)) return true;
        else return false;
    }
    //又是递归 采用代码《Maximum Depth of Binary Tree 》--https://leetcode.com/problems/maximum-depth-of-binary-tree/
    public int maxDepth(TreeNode root)
    {
        if(root == null)
        {
            return 0;
        }
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return l > r? l + 1:r+ 1;
    }
}
时间: 2024-11-10 19:16:23

[LeetCode][Java] Balanced Binary Tree的相关文章

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

leetcode -day18 Balanced Binary Tree

测试源码: //测试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace std; class base { public: base() { cout<<"base created!"<<endl; } ~base() { cout<<"base destroyed!"<<endl; } };

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

[LeetCode][JavaScript]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. https://leetco

leetCode 110. Balanced Binary Tree 平衡二叉树

110. 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

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. tags:深度优先遍历 c

【leetcode】Balanced Binary Tree(middle)

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. 思路: 我居然在这道题上卡了一个小时.关键是对于平衡的定义,我开始理解

Java for LeetCode 110 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. 解题思路: 递归即可,JAVA实现如下: public boolean

leetcode 110 Balanced Binary Tree ----- java

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. 判断一棵树是否是平衡二叉树 利用高度的答案,递归求解. /** * D