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;}
};
LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)相关推荐
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- leetCode 110. Balanced Binary Tree 平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110. Balanced Binary Tree 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...
- [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 ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode python3 简单题110. Balanced Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...
最新文章
- 2015年12月流量入口占比动态:仅直接访问实现上涨
- 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?
- 类与类之间的交互关系
- 处理get中的中文乱码情况
- rdlc报表 矩形高固定_固定资产条码管理系统特点分析
- 碰撞,处理碰撞,发射 Learn Unreal Engine (with C++)
- kafka监听topic消费_分布式专题|最近一直死磕kafka设计原理,都肝吐了
- Spark基础学习笔记11:Scala运算符
- MybatisPlus:使用SQL保留字(关键字)的操作
- 劝雷军别造车?王传福:别浪费钱和时间;华为面向全球招募天才少年:不限学历/学校;腾讯光子工作室强制不加班双休|极客头条...
- 这个时代,开发简单多了
- [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]
- PS透明婚纱抠图(超详细)
- Python3判断字符中英文数字符号
- Cell Host | 张群业/王哲/张澄-肠道微生物群失调加重腹主动脉瘤
- 机器学习实战(三)——决策树
- python zip(*zipped)的疑问
- Java常见面试题(181-200)
- 时间弥散谱成像和IMPULSED
- matlab mda,Matlab的学习最大似然估计,PCA和MDA的一段代码