【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.
题解:一A的题,写一个递归函数height计算树的高度,然后递归实现isBalance函数即可。代码如下:
/**
* 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(height(root->left)-height(root->right)) > )
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int height(TreeNode *root){
if(root == NULL)
return ;
int left = height(root->left);
int right = height(root->right);
return +(left>right?left:right);
}
};
【leetcode】Balanced Binary Tree的更多相关文章
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【Leetcode】【Easy】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
随机推荐
- UINavigationController改变动画效果
@interface UINavigationController (CustomTransition) - (void) pushWithCustomAnimation:(UIViewControl ...
- mysql 语句要求
mysql 语句不可以有单引号,要把单引号替换为双引号!
- 如何去掉MapReduce输出的默认分隔符
我们在用MapReduce做数据处理的时候,经常会遇到将只需要输出键或者值的情况,如context.write(new Text(record), new Text("")),这样 ...
- 本地文件上传到linux
首先下载插件,输入下面命令: yum -y install lrzsz 然后输入rz选择上传文件: rz 如果rz命令不好使的话,就输入: rz -be
- 经常使用socket函数具体解释
经常使用socket函数具体解释 关于socket函数,每一个的意义和基本功能都知道,但每次使用都会去百度,參数究竟是什么,返回值代表什么意义.就是说用的少,也记得不够精确. 每次都查半天.常常烦恼于 ...
- IPython introduction
转载:http://blog.csdn.net/gavin_john/article/details/53086766 1. IPython介绍 ipython是一个python的交互式shell,比 ...
- Python内置函数之super()
super(type[,object-or-type]) super()的作用在于类继承方面. 他可以实现不更改类内部代码,但是改变类的父类. 例子: 一般我们继承类的方式: >>> ...
- Android下的数据存储与访问、权限
弹出吐司 在onCreate中可以先获取控件对象 /data/data/程序的包名/ 在这个目录下面进行文件的读写可能因为包名的改变而变得不可靠. this可以是Activity,也 ...
- git add . git add -u git add -A命令区别图解
git版本不同会有所区别: Git Version 1.x: Git Version 2.x: git add . 修改(modified)以及新文件(new),但不包括被删除的文件. git ...
- Linux中crontab下scp文件传输的两种方式
Linux下文件传输一般有两个命令scp.ftp(工具需要下载安装) 本文主要讲讲scp的文件传输脚本 1.scp ssh-keygen -t rsa免输入密码,传输 这里假设主机A 用来获到主机B的 ...