【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,那么这棵树就是平衡二叉树。

【比较直观的Java代码】

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        if(root.left==null && root.right==null){
            return true;
        }
        if(Math.abs(depth(root.left)-depth(root.right)) > 1){
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }

    public int depth(TreeNode root){
        if(root==null){
            return 0;
        }
        return 1+Math.max(depth(root.left), depth(root.right));
    }
}

【改进后】

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        height(root);
        return run(root);
    }

    public boolean run(TreeNode root) {
        if (root == null) return true;

        int l = 0, r = 0;
        if (root.left != null) l = root.left.val;
        if (root.right != null) r = root.right.val;
        if (Math.abs(l - r) <= 1 && isBalanced(root.left) && isBalanced(root.right)) return true;

        return false;
    }

    public int height(TreeNode root) {
        if (root == null) return 0;
        root.val = Math.max( height(root.left), height(root.right) ) + 1;
        return root.val;
    }
}

利用了TreeNode结构中的val,用它来记录以当前结点为根的子树的高度,避免多次计算。

时间: 2024-10-08 20:37:25

【LeetCode】Balanced Binary Tree 解题报告的相关文章

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 @ Python

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

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

655. Print Binary Tree 解题报告(树)

第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]] Example 2: Input: 1 / 2 3 4 Output: [["", "", "", "1"

【LeetCode】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554 Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Explain 该题目相当简单,就是一个简单的二叉树左右对换结点. Solution solution 1 递归

[leetcode] 331. Verify Preorder Serialization of a Binary Tree 解题报告

题目链接: https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using