Skip to content

Commit 96ab7ee

Browse files
committed
week03_homework
1 parent 6df7198 commit 96ab7ee

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 根据一棵树的前序遍历与中序遍历构造二叉树。
2+
#
3+
# 注意:
4+
# 你可以假设树中没有重复的元素。
5+
#
6+
# 例如,给出
7+
#
8+
# 前序遍历 preorder = [3,9,20,15,7]
9+
# 中序遍历 inorder = [9,3,15,20,7]
10+
#
11+
# 返回如下的二叉树:
12+
#
13+
# 3
14+
# / \
15+
# 9 20
16+
# / \
17+
# 15 7
18+
# Related Topics 树 深度优先搜索 数组
19+
20+
21+
# leetcode submit region begin(Prohibit modification and deletion)
22+
# Definition for a binary tree node.
23+
# class TreeNode:
24+
# def __init__(self, x):
25+
# self.val = x
26+
# self.left = None
27+
# self.right = None
28+
29+
class Solution:
30+
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
31+
if not (preorder and inorder):
32+
return None
33+
root = TreeNode(preorder[0])
34+
mid_idx = inorder.index(preorder[0])
35+
root.left = self.buildTree(preorder[1:mid_idx+1], inorder[:mid_idx])
36+
root.right = self.buildTree(preorder[mid_idx+1:], inorder[mid_idx+1:])
37+
return root
38+
# leetcode submit region end(Prohibit modification and deletion)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
2+
#
3+
# 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(
4+
# 一个节点也可以是它自己的祖先)。”
5+
#
6+
# 例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]
7+
#
8+
#
9+
#
10+
#
11+
#
12+
# 示例 1:
13+
#
14+
# 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
15+
# 输出: 3
16+
# 解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
17+
#
18+
#
19+
# 示例 2:
20+
#
21+
# 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
22+
# 输出: 5
23+
# 解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
24+
#
25+
#
26+
#
27+
#
28+
# 说明:
29+
#
30+
#
31+
# 所有节点的值都是唯一的。
32+
# p、q 为不同节点且均存在于给定的二叉树中。
33+
#
34+
# Related Topics 树
35+
36+
37+
# leetcode submit region begin(Prohibit modification and deletion)
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
45+
class Solution:
46+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
47+
if not root or root == p or root == q: return root
48+
left = self.lowestCommonAncestor(root.left, p, q)
49+
right = self.lowestCommonAncestor(root.right, p, q)
50+
if not left: return right
51+
if not right: return left
52+
return root
53+
# leetcode submit region end(Prohibit modification and deletion)

Week03/NOTE.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
学习笔记
1+
学习笔记
2+
# 回溯
3+
回溯法采用试错的思想,它尝试分步的去解决一个问题。在分步解决问题的过程中,当它通过尝试发现现有的分步答案不能得到有效的正确的解答的时候,它将
4+
取消上一步甚至是上几步的计算,再通过其它的可能的分步解答再次尝试寻找问
5+
题的答案。
6+
回溯法通常用最简单的递归方法来实现,在反复重复上述的步骤后可能出现两种
7+
情况:
8+
9+
• 找到一个可能存在的正确的答案;
10+
11+
• 在尝试了所有可能的分步方法后宣告该问题没有答案。 在最坏的情况下,回溯法会导致一次复杂度为指数时间的计算。

0 commit comments

Comments
 (0)