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

刚开始,理解为完全二叉树了,即任意两层的深度差不超过1,想到用层次遍历,找到第一个没有左右孩子的结点所在的层,然后继续遍历,只要层差超过1就返回false,结果被打脸。例 {1,#,2,#,3}。后来又想到,只要有一个左右孩子为NULL就为最小层,然后喝整个层数去比,结果又无情的被大脸。例:{1,2,2,3,3,3,3,4,4,4,4,4,4,#,#,5,5}。究其所有,是没有理解平衡二叉树的概念。是任意结点的两子树的深度不超过1.,如{1,2,2,3,3,3,3,4,4,4,4,4,4,#,#,5,5},最大深度和最小深度超过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:
    bool isBalanced(TreeNode *root)
    {
        if(root==NULL)  return true;
        if(abs(getDepth(root->left)-getDepth(root->right))>1)
            return false;
        return isBalanced(root->left)&&isBalanced(root->right);
    }

    int getDepth(TreeNode *root)
    {
        if(root==NULL)  return 0;
        return 1+max(getDepth(root->left),getDepth(root->right));
    }
};

改进,来源LeetCode。改进的思路:上面的算法,需要计算深度时,每个结点都要计算一遍。这样就会影响效率,若是发现子树不平衡,则不计算具体的深度,而是直接返回-1。优化后,对每个结点获得左右子树的深度后,若是,平衡的返回真实深度,不然就返回-1.

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isBalanced(TreeNode *root)
13     {
14         if(root==NULL)   return true;
15         int l=getHeight(root->left);
16         int r=getHeight(root->right);
17
18         if(l<0||r<0||abs(l-r)>1)    return false;
19         return true;
20     }
21
22     int getHeight(TreeNode *root)
23     {
24         if(root==NULL)  return 0;
25         int l=getHeight(root->left);
26         int r=getHeight(root->right);
27
28         if(l<0||r<0||abs(l-r)>1)    return -1;  //表示不平衡的情况
29         return max(l,r)+1;
30     }
31 };
时间: 2024-10-10 18:02:12

[Leetcode] Balanced binary tree平衡二叉树的相关文章

[leetcode]Balanced Binary Tree @ Python

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

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. [题意] 判断二叉树是否是平衡二叉树 [思路] 平衡二叉树要

[CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. LeetCode上的原题,请参见我之前的博客Bala

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 110. Balanced Binary Tree 平衡二叉树

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. 题目大意: 判断一

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&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. //两次递归效率不高 合并看leetcode代码 /** * Defi