LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

翻译

给定一个二叉树,决定它是否是高度平衡的。

(高度是名词不是形容词……

对于这个问题。一个高度平衡二叉树被定义为:

这棵树的每一个节点的两个子树的深度差不能超过1。

原文

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,考察各种边界条件
2,考察递归以及对树的遍历
3。考察求解树的高度

先从小的模块開始写。也就是树的高度。

事实上我看以下的代码,或者说这几天的代码。都是怎么看怎么不顺眼,不知道是不是由于天气太冷让我思维都和身体一样僵硬了。

今天中雪……明天就要达到老家的历史最低温了。

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

通过不断的从上往下的递归来求出它的高度。由于是求最大的高度,所以用了max函数。

由于上面的函数是求的一个节点以下的最大深度。而不包含该节点。所以在以下的函数中用了三目运算符来求出当前节点的深度。后面继续使用了abs函数来求绝对值。

接着就是继续递归了。假设有一步(一个节点)不满足条件,那么就直接返回假了 (不妥协……

bool isBalanced(TreeNode* root) {
    if (!root || (!root->left && !root->right))  return true;
    int left = root->left == NULL ? 0 : getHeight(root->left) + 1;
    int right = root->right == NULL ? 0 : getHeight(root->right) + 1;
    if (abs(left - right) > 1)
        return false;
    else  if (!isBalanced(root->left) || !isBalanced(root->right))
        return false;
    return true;
}

这道题我觉得还是蛮难的。还是要多重复的琢磨琢磨了。

代码

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
    int getHeight(TreeNode* root) {
        int left = 0, right = 0;
        if (!root || (!root->left &&!root->right))
            return 0;
        if (root->left != NULL)
            left = 1 + getHeight(root->left);
        if (root->right != NULL)
            right = 1 + getHeight(root->right);
        return max(left, right);
    }

    bool isBalanced(TreeNode* root) {
        if (!root || (!root->left && !root->right))         return true;
        int left = root->left == NULL ? 0 : getHeight(root->left) + 1;
        int right = root->right == NULL ? 0 : getHeight(root->right) + 1;
        if (abs(left - right) > 1)
            return false;
        else  if (!isBalanced(root->left) || !isBalanced(root->right))
            return false;
        return true;
    }
};
时间: 2024-10-16 05:30:57

LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)的相关文章

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 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 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. 思路:判断是否是平衡二叉树,dfs即可,递归判断每个子树是否平衡二叉树

110 Balanced Binary Tree 平衡二叉树

给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7]:    3   / \  9  20    /  \   15   7返回 true .案例 2:给出二叉树 [1,2,2,3,3,null,null,4,4]:       1      / \     2   2    / \   3   3  / \ 4   4返回 false .详见:ht

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. 判断一棵树是否属于平衡二叉树 判断主节点的左右节点深度大小差,如果不在

leetcode 110 Balanced Binary Tree ----- 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. 判断一棵树是否是平衡二叉树 利用高度的答案,递归求解. /** * D

Java for 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. 解题思路: 递归即可,JAVA实现如下: public boolean

Java [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. 解题思路: 递归法解题. 代码如下: /** * Defi

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