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] 113. Path Sum II

[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的更多相关文章

  1. [LeetCode] 系统刷题5_Dynamic Programming

    Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...

  2. [LeetCode] 系统刷题1_代码风格及边界

    代码风格 说自己不清楚的算法,比如KMP,如果解释不清楚或者写不出来的算法建议不提 注意代码的缩进以及空格的合理运用,使得代码看起来比较整洁有条理 注意边界的条件以及越界 误区: 算法想出来还仅仅不够 ...

  3. [LeetCode] 系统刷题3_Binary search

    可以参考 [LeetCode] questions conclusion_ Binary Search

  4. [LeetCode] 系统刷题2_排列组合

    要用到backtracking,是否要跟backtracking放到一起总结? 适用范围: 几乎所有搜索问题 什么时候输出 哪些情况需要跳过 相关题目: [LeetCode] 78. Subsets ...

  5. [LeetCode] 系统刷题6_Linked List

    1. Dummy Node 2. Basic skills [LeetCode] 206. Reverse Linked List_Easy tag: Linked List 2. Fast slow ...

  6. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

  7. leetcode top-100-liked-questions刷题总结

    一.起因 宅在家中,不知该做点什么.没有很好的想法,自己一直想提升技能,语言基础自不必言,数据结构还算熟悉,算法能力一般.于是乎,就去刷一通题. 刷题平台有很多,我选择了在leetcode进行刷题.回 ...

  8. LeetCode 高效刷题路径

    LeetCode 高效刷题路径 Hot 100 https://leetcode.com/problemset/hot-100/ https://leetcode-cn.com/problemset/ ...

  9. leetcode 算法刷题(一)

    今天开始刷Leetcode上面的算法题.我会更新我刷题过程中提交的代码(成功和不成功的都有)和比较好的解法 第二题 Add Two Numbers 题目的意思:输入两个链表,这两个链表都是倒序的数字, ...

随机推荐

  1. Linux常用指令笔记

    目标:统计当前目录下java文件的个数 指令:`ls -R ./ | grep .java$ | wc -l` 原理:`ls -R ./`列出当前文件夹下的所有FILE,包括目录以及文件;`grep ...

  2. docker容器运行与退出

    #下载centos镜像,运行一个名为mycentos的容器,并在容器里运行/bin/bash docker run -ti --name mycentos centos /bin/bash #退出 e ...

  3. halcon开发必读

    关于HALCON的新手入门问题简答(1) 无论读入什么图像,读入图像显示效果明显和原始图像不一致,哪怕是从相机读入的图像,也是明显颜色差异.什么原因引起? 答:初步诊断是,显示的时候调用的颜色查找表存 ...

  4. oracle查看某表字段类型

    来源:https://www.cnblogs.com/ufindme/p/5033843.html 今天遇到一个问题:要求在可重复执行的SQL脚本添加一段SQL代码:修改当前的数据类型.因为SQL代码 ...

  5. golang加油!

  6. SQL in、not in、exists和not exists的区别:

    来自:http://blog.sina.com.cn/s/blog_8a0c4f130100zaw2.html 先谈谈in和exists的区别: exists:存在,后面一般都是子查询,当子查询返回行 ...

  7. Window 产品密钥

    2019.4.2 测试可用 window2003         DF74D-TWR86-D3F4V-M8D8J-WTT7M

  8. 局域网ARP攻击防护

    通过借助一些安全软件来实现局域网ARP检测及防御功能. A.电脑管家 电脑管家--工具箱--下载ARP防火墙模块 不支持window2003 B.服务器安全狗 Windows版下载:http://fr ...

  9. How to write threats to validity?

    Paper reference Threats to construct validity are concerned with the relationship between theory and ...

  10. 解决macOS git clone Azure DevOps提示身份认证失败问题

    macOS的终端输入git clone对应Azure DevOps的Git地址,紧接着输入正确的用户名和密码仍然会提示认证失败.解决的方法是安装微软的Git-Credential-Manager.由于 ...