【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 ...
随机推荐
- Calculation 2-欧拉函数的运用
Calculation 2 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit ...
- rsync断点续传
这经常是我们所说的镜像同步就是这么来的,如果断点续传呢?rsync完全可以做到这一点.man手册再次告诉我们: --partial By default, rsync will delete ...
- java之Cookie具体解释
Cookie是由server端生成.发送给User-Agent(通常是浏览器).浏览器会将Cookie的key/value保存到某个文件夹下的文本文件内.下次请求同一站点时就发送该Cookie给ser ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(五)
无书面许可请勿转载 高级Playbook Extra variables You may have seen in our template example in the previous chapt ...
- iOS自动化构建 xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/D...
报这个错误的原因是xcode-select不在默认的路径 1.找到xcode-select的当前路径终端命令行 xcode-select --print-path /Library/Developer ...
- tigervnc环境搭建
在root用户下执行以下操作: 1.安装tigervnc yum install tigervnc tigervnc-server 2.配置tigervnc 编辑vncservers文件,执行如下命令 ...
- mac 编译ffmpeg真简单!
brew info ffmpeg 有安装选项,并且会提示依赖库安装状态 安装依赖库 brew install automake fdk-aac git lame libass libtool libv ...
- java学习日历输出(一)
import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.GregorianCalendar; i ...
- Python中使用 Selenium 实现网页截图实例
Selenium 是一个可以让浏览器自动化地执行一系列任务的工具,常用于自动化测试.不过,也可以用来给网页截图.目前,它支持 Java.C#.Ruby 以及 Python 四种客户端语言.如果你使用 ...
- esper 附录A
A2 select irstream symbol, volume, price from MarketDate.win.time(5.5 sec) seconds seconds seconds s ...