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

代码实现

class Solution
{
public:
    bool isBalanced(TreeNode *root)
        {
            return balancedHeight (root)>=0;
        }
        int balancedHeight (TreeNode* root)
        {
            if  (root==NULL)      return 0;
            int left=balancedHeight (root->left);
            int right=balancedHeight(root->right);

            if(left<0 || right<0 ||abs(left - right)>1)   return -1;

            return max(left,right)+1;
        }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-30 10:59:48

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]Balanced Binary Tree @ Python

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

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

LeetCode&mdash;&mdash;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

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. 这道题以前整理过,这里就不细写了,直接贴上代码: /** * Defi