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.

【题意】

判断二叉树是否是平衡二叉树

【思路】

平衡二叉树要求任意一个节点的左右子树高度差的绝对值不大于1.

按照定义,递归求解即可。

【代码】

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    int getHeight(TreeNode *root){
        if(root==NULL)return 0;

        //获得左子树高度
        int leftHeight=getHeight(root->left);
        if(leftHeight==-1)return -1;
        //获得右子树高度
        int rightHeight=getHeight(root->right);
        if(rightHeight==-1)return -1;

        //判断左右子树高度差是否符合要求, 如果符合要求,则返回当前二叉树的高,如果不符合要求,则返回-1
        if(abs(leftHeight-rightHeight)<=1)
            return max(leftHeight, rightHeight)+1;

        return -1;
    }

    bool isBalanced(TreeNode *root) {
        if(root==NULL)return true;

        int height=getHeight(root);
        if(height==-1)return false;
        return true;

    }
};

LeetCode: Balanced Binary Tree [110]

时间: 2024-08-08 22:06:10

LeetCode: Balanced Binary Tree [110]的相关文章

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 解题报告

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

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. 思路 判断一棵树是不是平衡二叉树,递归的求每个结点的左子树和