[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
The most important : [LeetCode] questions conclustion_BFS, DFS
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历。
此处说明Divide and Conquer 的做法,其实跟recursive的做法很像,但是将结果存进array并且输出,最后conquer (这一步worst T:O(n)) 起来,所以时间复杂度可以从遍历O(n) -> O(n^2).
实际上代码是一样, 就是把[root.val] 放在先, 中, 后就是pre, in, post order了.
1) Preorder traversal
class Solution:
def preOrder(self, root):
if not root: return []
left = self.preOrder(root.left)
right = self.preOrder(root.right)
return [root.val] + left + right # worst T: O(n)
2)
Inorder traversal
class Solution:
def inOrder(self, root):
if not root: return []
left = self.preOrder(root.left)
right = self.preOrder(root.right)
return left + [root.val] + right # worst T: O(n)
3) Postorder traversal
class Solution:
def postOrder(self, root):
if not root: return []
left = self.preOrder(root.left)
right = self.preOrder(root.right)
return left + right + [root.val] # worst T: O(n)
[LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS
[LeetCode] 110. Balanced Binary Tree_Easy tag: DFS
[LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
[LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS
[LeetCode] 112. Path Sum_Easy tag: DFS
[LeetCode] 437. Path Sum III_ Easy tag: DFS
[LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
[LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive
[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive
[LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Binary Search Tree
[LeetCode] 98. Validate Binary Search Tree_Medium
Search Range in a Binary Search Tree
[LeetCode] 700. Search in a Binary Search Treer_Easy_tag: Binary Search Tree
[LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal
[LeetCode] 173. Binary Search Tree Iterator_Medium_tag: Binary Search Tree
[LeetCode] 701. Insert into a Binary Search Tree_Medium_tag: Binary Search Tree
Remove Node in Binary Search Tree
Steps:
1. Find the node
2. Find the maximum node in the left
subtree
3. Replace the node with the maximum
node in the left subtree.
Special Cases:
1. The node doest have a left child.
2. The maximum node in the left subtree
has a left child.
3. The node is the root of the tree.
[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的更多相关文章
- [LeetCode] 系统刷题5_Dynamic Programming
Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...
- [LeetCode] 系统刷题1_代码风格及边界
代码风格 说自己不清楚的算法,比如KMP,如果解释不清楚或者写不出来的算法建议不提 注意代码的缩进以及空格的合理运用,使得代码看起来比较整洁有条理 注意边界的条件以及越界 误区: 算法想出来还仅仅不够 ...
- [LeetCode] 系统刷题3_Binary search
可以参考 [LeetCode] questions conclusion_ Binary Search
- [LeetCode] 系统刷题2_排列组合
要用到backtracking,是否要跟backtracking放到一起总结? 适用范围: 几乎所有搜索问题 什么时候输出 哪些情况需要跳过 相关题目: [LeetCode] 78. Subsets ...
- [LeetCode] 系统刷题6_Linked List
1. Dummy Node 2. Basic skills [LeetCode] 206. Reverse Linked List_Easy tag: Linked List 2. Fast slow ...
- LeetCode的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
- leetcode top-100-liked-questions刷题总结
一.起因 宅在家中,不知该做点什么.没有很好的想法,自己一直想提升技能,语言基础自不必言,数据结构还算熟悉,算法能力一般.于是乎,就去刷一通题. 刷题平台有很多,我选择了在leetcode进行刷题.回 ...
- LeetCode 高效刷题路径
LeetCode 高效刷题路径 Hot 100 https://leetcode.com/problemset/hot-100/ https://leetcode-cn.com/problemset/ ...
- leetcode 算法刷题(一)
今天开始刷Leetcode上面的算法题.我会更新我刷题过程中提交的代码(成功和不成功的都有)和比较好的解法 第二题 Add Two Numbers 题目的意思:输入两个链表,这两个链表都是倒序的数字, ...
随机推荐
- .net core 2.0 虚拟目录下载 Android Apk 等文件
当Android 文件 Apk 放在Asp.net core wwwroot 虚拟目录下面.访问是 404,设置Content-Type类型 app.UseStaticFiles(); //设置实际目 ...
- python3 log 日志记录
在调试的过程中,很多地方需要用到日志 如下 import logging LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s&qu ...
- go语言fallthrough的用法心得
fallthrough:Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面 ...
- 【nodejs】初识 NodeJS(四)
上节我们把服务器.路由和请求处理程序结合在一起了,下面就编写一个具体的 web 应用. 上传图片的 web 应用 服务器模块(server.js) var http = require('http') ...
- jenkins GitHub 自动触发
jenkins GitHub 自动触发 转载请注明出处: 转载自Bin's Blog: jenkins GitHub 自动触发( http://www.wenbin.cf/post/54/ ) 需要 ...
- EditText小记
今天在编写样式的时候,需要设置数据输入为单行,但是 Android:singleLine=”true” 显示为已过期,提示使用 android:maxLines=“1” 代替,但是设置后却发现并没有效 ...
- SQL Server CTE 递归查询全解
在TSQL脚本中,也能实现递归查询,SQL Server提供CTE(Common Table Expression),只需要编写少量的代码,就能实现递归查询,本文详细介绍CTE递归调用的特性和使用示例 ...
- dir 命令手册
dir 命令手册 参数 /A D 目录 R 只读文件 H 隐藏文件 A 准备存档的文件 S 系统文件 - 表示"否"的前缀 /B 使用空格式(没有标题信息或摘要) /C 在文件大小 ...
- Java基础知识点总结
前言 本文主要是我之前复习Java基础原理过程中写的Java基础知识点总结.Java的知识点其实非常多,并且有些知识点比较难以理解,有时候我们自以为理解了某些内容,其实可能只是停留在表面上,没有理解其 ...
- [原]Jenkins(二十一) jenkins再出发Build periodically和Poll SCM
缘由: 使用jenkins的目的需要固定时间构建和间隔固定时间构建,所以才会用到这两个功能. 位置:这两个功能的位置位于每个job的config项中,如下图: [重要的注意点:] ...