【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution
[LeetCode]
https://leetcode.com/submissions/detail/40087813/
Total Accepted: 72203 Total Submissions: 225370 Difficulty: Easy
Question
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.
https://leetcode.com/submissions/detail/40087813/
Examples
–
Ways
运用递归,递归当前和 左子树和右子树的深度,当所有的左右子树的深度相差不超过1的时候,就是平衡二叉树。
Better solution: 每次计算树的深度的时候保存下来树的深度到一个HashMap里,下次寻找的时候先到 HashMap 里查找,找不到在进行计算,然后继续保存。优化算法后,是算法复杂度降到了O(n)。
Solution
托管在我的GitHub上:
https://github.com/fuxuemingzhu/BalancedTreePro
Captures
测试结果截图:
Reference
这个帮助对我很大:
http://www.cnblogs.com/Antech/p/3705928.html
Date
2015/9/17 0:50:10
【LeetCode】Balanced Binary Tree 算法优化 解题报告的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】536. Construct Binary Tree from String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计字符串出现的次数 日期 题目地址:https:// ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- 有限元边界 Dirichlet 条件处理
参考自百度文档,这里只考虑 Dirichlet 边界条件情况. 有限元法基本方法就是是构造线性方程组 \[\begin{equation} Au = f \end{equation}\] 进行求解.其 ...
- TP、PHP同域不同子级域名共享Session、单点登录
TP.PHP同域不同子级域名共享Session.单点登录 目的: 为了部署同个域名下不同子级域名共享会话,从而实现单点登录的问题,一处登录,同域处处子系统即可以实现自动登录. PHP支持通过设置coo ...
- Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议
Mysql查询优化汇总 order by优化例子,group by优化例子,limit优化例子,优化建议 索引 索引是一种存储引擎快速查询记录的一种数据结构. 注意 MYSQL一次查询只能使用一个索引 ...
- 【Python小试】将核酸序列翻译成氨基酸序列
三联密码表 gencode = { 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M', 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT' ...
- Python函数之传参
目录 1. 函数传参 1.1 参数的作用 1.2 形参和实参 1.3 位置参数 1.4 关键字参数 1.5 默认实参 1.6 参数总结 2. 可变参数 1. 函数传参 1.1 参数的作用 1.2 形参 ...
- 日常Java 2021/10/31
泛型类 泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分.和迈型方法一样,泛型类的类型参数声明部分也包含一个或多个类型参数,参数间用逗号隔开.一个泛型参数,也被称为一个类型变量, ...
- CAD简介
Computer-aided design (CAD) is the use of computers (or workstations) to aid in the creation, modifi ...
- 创建Oracle数据库实例
创建Oracle数据库实例 转自oracle数据库创建实例 数据库已经安装完成,可以正常登陆查看用户等操作. system用户只能用normal身份登陆em.除非你对它授予了sysdba的系统权限或者 ...
- Linux学习 - 环境变量配置文件
一.环境变量配置文件的作用 /etc/profile /etc/profile.d/*.sh ~/.bash_profile ~/.bashrc /etc/bashrc 1 /etc/profile的 ...
- Linux:变量$#,$@,$0,$1,$2,$*,$$,$?
写一个简单的脚本 vim var 脚本内容如下: #!/bin/sh echo "the number of parameters passed to the script: $#" ...