【LeetCode 110_二叉树_遍历】Balanced Binary Tree

解法一:From top to bottom
int treeHeight(TreeNode *T)
{
if (T == NULL)
return ; return max(treeHeight(T->left), treeHeight(T->right)) + ;
} bool isBalanced(TreeNode* root)
{
if (root == NULL)
return true; int left_height = treeHeight(root->left);
int right_height = treeHeight(root->right);
if (abs(left_height - right_height) > )
return false; if (!isBalanced(root->left) || !isBalanced(root->right))
return false; return true;
}
解法一递归判断子树是否为平衡二叉树的过程中,重复计算了多次子树的高度,时间复杂度大于O(N)。解法二将这个计算过程优化了,时间复杂度为O(N)。
解法二:From bottom to top
int dfsTreeHeight(TreeNode *T)
{
if (T == NULL)
return ; int left_height = dfsTreeHeight(T->left);
if (left_height == -)
return -;
int right_height = dfsTreeHeight(T->right);
if (right_height == -)
return -;
if (abs(left_height - right_height) > )
return -; return max(left_height, right_height) + ;
} bool isBalanced(TreeNode* root)
{
if (root == NULL)
return true; return dfsTreeHeight(root) != -;
}
【LeetCode 110_二叉树_遍历】Balanced Binary Tree的更多相关文章
- 【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal
解法一:非递归 vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == NUL ...
- 【LeetCode 100_二叉树_遍历】Same Tree
解法一:递归 bool isSameTree(TreeNode* p, TreeNode* q) { if (p == NULL && q == NULL) return true; ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- 【Leetcode】【Easy】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...
- 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
解法一:递归 int minDepth(TreeNode* root) { if (root == NULL) ; if (root->left == NULL) { ; } else if ( ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- centos7开启网卡功能
centos7安装完成后,网卡默认是关闭的,未分配ip地址 解决办法: 1.cd /etc/sysconfig/network-scripts/ 2.ls查看网卡 3.修改该文件 vi ifcfg-e ...
- Django学习笔记之URL标签的使用
期初用django 开发应用的时候,完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRedirect()也是硬编码转向地址,当然在template 中也是一样了, ...
- RabbitMQ学习之(五)_一个基于PHP的RabbitMQ操作类
//amqp.php类文件 <?php class Amqp { public $e_name; public $q_name; public $k_route; public $channel ...
- GIT使用—创建并使用远程版本库
远程版本库 (1)创建一个裸版本库 [root@localhost tmp]# git init fluff2 Initialized empty Git repository in /tmp/flu ...
- 20145335郝昊《java程序设计》第9周学习总结
20145335郝昊 <Java程序设计>第9周学习总结 教材学习内容总结 第16章 JDBC(Java DataBase Connectivity)即java数据库连接,是一种用于执行S ...
- 20135320赵瀚青LINUX第五周学习笔记
赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 概述 按照刘老师的周从三个角 ...
- <linux/init.h>,<linux/module.h>头文件不存在等问题的解决方法
这个问题真心是处理了一个下午,还自己去下载了个最新的内核拿来编译,其实是完全没必要的,因为ubuntu系统是可以直接下载新内核的. 你可以在/usr/src/文件夹下找到这些内核文件夹,比如说我自己的 ...
- hbase(二)hfile结构
HFile结构 截止hbase 1.0.2版本,hfile已经有3个版本,要深入了解hfile的话,还是要从第一个版本开始看起. hfile v1 Data Block:保存表中的数据,这部分可以被压 ...
- 使用淘宝的npm代理下载模块
npm install node-sass --registry=http://registry.npm.taobao.org
- Chrome好用的插件:Wappalyzer 检测网站使用的技术
Chrome好用的插件:Wappalyzer 检测网站使用的技术 Wappalyzer是一款能够分析目标网站所采用的平台架构.网站环境.服务器配置环境.javascript框架.编程语言等参数的chr ...