翻译

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

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

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

这棵树的每一个节点的两个子树的深度差不能超过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;
}
};

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

  1. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  2. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  3. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  4. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

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

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. LeetCode 110. Balanced Binary Tree(判断平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  8. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  9. 110 Balanced Binary Tree 平衡二叉树

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

随机推荐

  1. HBase EndPoint加载失败

    概述 参考博客(http://blog.csdn.net/carl810224/article/details/52224441)编写EndPoint协处理器,编写完成后使用Maven打包(使用ass ...

  2. easyui 属性集合

    easyUI属性汇总 属性分为CSS片段和JS片段. CSS类定义:1.div easyui-window 生成一个window窗口样式. 属性如下: 1)modal:是否生成模态窗口.true[是] ...

  3. git fetch, git pull 以及 FETCH_HEAD

    git push. 这个很简单, 其实和后面的差不多, 这里就不讲了. 唯一需要注意的地方是: git push origin :branch2, 表示将一个内容为空的同名分支推送到远程的分支.(说白 ...

  4. 程序员应该知道的几个国外IT网站

    程序员应该知道的几个国外IT网站   摘要:文中总结了几个常用的国外IT网站,下面列举出来供大家学习参考: 导读:文中总结了几个常用的国外IT网站,下面列举出来供大家学习参考: 1. TheServe ...

  5. Cascode MOSFET increases boost regulator's input- and output-voltage ranges

    Targeting use in portable-system applications that require raising a battery's voltage to a higher l ...

  6. Programmer in Google Code

    https://code.google.com/p/vsprog/ CLI program support USB_TO_XXX of Versaloon. Support AVR_ISP/JTAG, ...

  7. Druid 配置 wallfilter

    这个文档提供基于Spring的各种配置方式 使用缺省配置的WallFilter <bean id="dataSource" class="com.alibaba.d ...

  8. linux下svn的用法

    转载:http://blog.chinaunix.net/uid-22150747-id-189264.html 1.将文件checkout到本地目录 svn checkout path(path是服 ...

  9. 笔记本如何查看mac地址

    最近有网友咨询他的笔记本如何查看mac地址,一般情况当我们需要用mac地址,ip地址,本地dns的时候都可以参考下面的方法   MAC地址又称为网卡的物理地址,每台电脑都有一个唯一的MAC地址,也正因 ...

  10. FAQ:领域服务和应用服务的职责是什么?

    问答部分 问: 领域服务的职责是什么? 答: 夸聚合实例业务逻辑. 没办法合理放到实体中的其它业务逻辑. 问: 领域服务的设计原则是什么? 答: 用来组织业务逻辑. 面向业务逻辑. 细粒度. 内部视图 ...