[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.

这道题以前整理过,这里就不细写了,直接贴上代码:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool fun(TreeNode* root,int& depth){
        if(root==NULL){
            depth=0;
            return true;
        }
        int left,right;
        if(fun(root->left,left)&&fun(root->right,right)){
            int diff=left-right;
            if(abs(diff)<=1){
                depth=1+(left>right?left:right);
                return true;
            }
        }
        return false;
    }
    bool isBalanced(TreeNode *root) {
        int depth=0;
        return fun(root,depth);
    }
};

下面是以前整理的链接:平衡二叉树的判断

时间: 2024-08-03 11:25:28

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