[Lintcode]93. Balanced Binary Tree/[Leetcode]

93. Balanced Binary Tree/

  • 本题难度: Easy
  • Topic: Binary Tree

Description

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.

Example

Example 1:

Input: tree = {1,2,3}

Output: true

Explanation:
This is a balanced binary tree.
      1
     / \
    2  3

Example 2:

Input: tree = {3,9,20,#,#,15,7}

Output: true

Explanation:
This is a balanced binary tree.
      3
     / \
    9  20
      /  \
     15   7 

Example 3:

Input: tree = {1,#,2,3,4}

Output: false

Explanation:
This is not a balanced tree.
The height of node 1‘s right sub-tree is 2 but left sub-tree is 0.
      1
       \
       2
      /  \
     3   4

我的代码

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: The root of binary tree.
    @return: True if this Binary tree is Balanced, or false.
    """

    def balancehigh(self,root):
        if root is None:
            return 0
        left = self.balancehigh(root.left)
        right = self.balancehigh(root.right)
        if left == -1 or right == -1 or abs(left-right)>1:
            return -1
        return 1+max(left,right)

    def isBalanced(self, root):
        # write your code here
        return self.balancehigh(root)!=-1
 

思路

仍然是递归。

当一个子树不平衡时,整个二叉树肯定不平衡,否则记录其高度。

原文地址:https://www.cnblogs.com/siriusli/p/10376639.html

时间: 2024-07-31 16:57:16

[Lintcode]93. Balanced Binary Tree/[Leetcode]的相关文章

Balanced Binary Tree leetcode 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. 题解: 采用递归的方法,要记录depth用来比较. 代码如下:

110.Balanced Binary Tree 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. 很早以前做的了  准

Balanced Binary Tree Leetcode

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. 这道题一开始的做法比较简单粗暴. public class Solut

Balanced Binary Tree --Leetcode C++

递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { public: bool isBalanced(TreeNode* root) { if(root==NULL){ return true; } int ldepth=depth(root->left); int rdepth=depth(root->right); if(abs(ldepth-rdepth

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

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