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));
    }
}
时间: 2024-11-08 22:31:59

LeetCode题解: 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题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

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 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative

[LeetCode 题解]: Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

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][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. 题意: 给定一棵二叉树,判定它是否为平衡二叉树. 算法分析:

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