Skip to content

Commit dd46347

Browse files
committed
2.21
1 parent 199fb02 commit dd46347

File tree

7 files changed

+372
-4
lines changed

7 files changed

+372
-4
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515
- [链表中倒数第k个节点](./SwordToOffer/Doc/链表中倒数第k个节点.md)
1616
- [反转链表](./SwordToOffer/Doc/反转链表.md)
1717
- [合并两个排序的链表](./SwordToOffer/Doc/合并两个排序的链表.md)
18-
- [树的子结构](./SwordToOffer/Doc/树的子结构.md)
18+
- [***树的子结构***](./SwordToOffer/Doc/树的子结构.md)
19+
- [***树的镜像***](./SwordToOffer/Doc/树的镜像.md)
20+
- [顺时针打印矩阵](./SwordToOffer/Doc/顺时针打印矩阵.md)
21+
22+
1923

SwordToOffer/Code/16.py

+60-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,69 @@ def __init__(self, x):
55
self.left = None
66
self.right = None
77

8+
def FirstBrowse(self, pRoot):
9+
result = []
10+
if pRoot == None:
11+
return result
12+
else:
13+
result.append(pRoot.val)
14+
result = result + self.FirstBrowse(pRoot.left)
15+
result = result + self.FirstBrowse(pRoot.right)
16+
return result
817

918
class Solution:
1019
def HasSubtree(self, pRoot1, pRoot2):
1120
# write code here
12-
if pRoot1==None or pRoot2==None:
21+
if pRoot1 == None or pRoot2 == None:
1322
return False
23+
flag = False
24+
if pRoot1.val == pRoot2.val:
25+
flag = self.doseNode1HasNode2(pRoot1, pRoot2)
1426

15-
27+
if not flag:
28+
flag = self.HasSubtree(pRoot1.left, pRoot2)
29+
30+
if not flag:
31+
flag = self.HasSubtree(pRoot1.right, pRoot2)
32+
33+
return flag
34+
35+
def doseNode1HasNode2(self, Node1, Node2):
36+
if Node2 == None:
37+
return True
38+
if Node1 == None:
39+
return False
40+
if Node1.val != Node2.val:
41+
return False
42+
43+
return self.doseNode1HasNode2(Node1.left, Node2.left) and self.doseNode1HasNode2(Node1.right, Node2.right)
44+
45+
46+
pRoot1 = TreeNode(1)
47+
treeNode1 = TreeNode(2)
48+
treeNode2 = TreeNode(3)
49+
treeNode3 = TreeNode(4)
50+
treeNode4 = TreeNode(5)
51+
treeNode5 = TreeNode(6)
52+
treeNode6 = TreeNode(7)
53+
pRoot1.left = treeNode1
54+
pRoot1.right = treeNode2
55+
treeNode1.left = treeNode3
56+
treeNode1.right = treeNode4
57+
treeNode2.left = treeNode5
58+
treeNode2.right = treeNode6
59+
60+
pRoot2 = TreeNode(1)
61+
treeNode1b = TreeNode(2)
62+
treeNode2b = TreeNode(3)
63+
treeNode3b = TreeNode(4)
64+
treeNode4b = TreeNode(6)
65+
treeNode5b = TreeNode(7)
66+
pRoot2.left = treeNode1b
67+
pRoot2.right = treeNode2b
68+
treeNode2b.left = treeNode3b
69+
treeNode2b.right = treeNode4b
70+
treeNode3b.left = treeNode5b
71+
72+
solution = Solution()
73+
print solution.HasSubtree(pRoot1, pRoot2)

SwordToOffer/Code/17.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding:utf-8 -*-
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
9+
class Solution:
10+
# 返回镜像树的根节点
11+
def Mirror(self, root):
12+
# write code here
13+
if root == None:
14+
return None
15+
if root.left == None or root.right == None:
16+
return self.change(root)
17+
18+
if root.left.left is None and root.left.right is None and root.right.left is None and root.right.right is None:
19+
return self.change(root)
20+
else:
21+
mirrorRoot = TreeNode(root.val)
22+
mirrorRoot.left = self.Mirror(root.right)
23+
mirrorRoot.right = self.Mirror(root.left)
24+
return mirrorRoot
25+
26+
def Mirror2(self, root):
27+
# write code here
28+
if not root:
29+
return root
30+
node = root.left
31+
root.left = root.right
32+
root.right = node
33+
self.Mirror2(root.left)
34+
self.Mirror2(root.right)
35+
return root
36+
37+
38+
def Mirror3(self,root):
39+
if root: # 如果存在根节点
40+
root.left, root.right = root.right, root.left # 根节点左右交换,俩变量交换也可以这样写
41+
self.Mirror(root.left) # 递归调用左节点
42+
self.Mirror(root.right) # 递归调用右节点
43+
return root # 返回节点
44+
else:
45+
return # else无节点时直接return
46+
47+
def change(self, Node):
48+
newNode = TreeNode(Node.val)
49+
newNode.left = Node.right
50+
newNode.right = Node.left
51+
return newNode
52+
53+
54+
def FirstBrowse(pRoot):
55+
result = []
56+
if pRoot == None:
57+
return result
58+
else:
59+
result.append(pRoot.val)
60+
result = result + FirstBrowse(pRoot.left)
61+
result = result + FirstBrowse(pRoot.right)
62+
return result
63+
64+
65+
pRoot8 = TreeNode(8)
66+
treeNode6 = TreeNode(6)
67+
treeNode10 = TreeNode(10)
68+
treeNode5 = TreeNode(5)
69+
treeNode7 = TreeNode(7)
70+
treeNode9 = TreeNode(9)
71+
treeNode11 = TreeNode(11)
72+
73+
pRoot8.left = treeNode6
74+
pRoot8.right = treeNode10
75+
treeNode6.left = treeNode5
76+
treeNode6.right = treeNode7
77+
treeNode10.left = treeNode9
78+
treeNode10.right = treeNode11
79+
s = Solution()
80+
t = s.Mirror(pRoot8)
81+
t2 = s.Mirror2(pRoot8)
82+
t3 = s.Mirror2(pRoot8)
83+
print FirstBrowse(pRoot8)
84+
print FirstBrowse(t)
85+
print FirstBrowse(t2)
86+
print FirstBrowse(t3)

SwordToOffer/Code/18.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding:utf-8 -*-
2+
import numpy as np
3+
4+
5+
class Solution:
6+
7+
8+
# matrix类型为二维列表,需要返回列表
9+
def printMatrix(self, matrix):
10+
# write code here
11+
size = len(matrix)
12+
size = int(np.sqrt(size))
13+
matrix = np.reshape(np.array(matrix), newshape=(size, size))
14+
15+
newArray = []
16+
17+
for index1 in range(0, int(size / 2)):
18+
for index2 in range(index1, size - index1 - 1):
19+
newArray.append(matrix[index1, index2])
20+
for index3 in range(index1, size - index1 - 1):
21+
newArray.append(matrix[index3, size - index1 - 1])
22+
for index4 in range(size - index1 - 1, index1, -1):
23+
newArray.append(matrix[size - index1 - 1, index4])
24+
for index5 in range(size - index1 - 1, index1, -1):
25+
newArray.append(matrix[index5, index1])
26+
27+
return newArray
28+
29+
def reversematrix(self,matrix):
30+
s=matrix.shape
31+
newmatrix=[]
32+
for i in range(s[1]-1,-1,-1):
33+
a=[]
34+
for j in range(s[0]):
35+
a.append(matrix[j,i])
36+
newmatrix.append(a)
37+
newmatrix=np.array(newmatrix)
38+
return newmatrix
39+
40+
def printMatrix2(self, matrix):
41+
newArray=[]
42+
matrix=np.array(matrix)
43+
print matrix
44+
while len(matrix)!=0:
45+
newArray.extend(matrix[0])
46+
matrix=matrix[1:]
47+
matrix=self.reversematrix(matrix)
48+
return newArray
49+
50+
51+
s = Solution()
52+
s.printMatrix2([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])

SwordToOffer/Doc/树的子结构.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,48 @@
66
## 题目描述
77
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
88
## 解题思路
9+
本来的思路是采用任一一种遍历方式对两棵树同时进行遍历,然后看是否是"子串"关系,但是好像这个思路并行不通。
10+
正确思路应该是:
11+
算法1:
12+
先判断当前两棵树的根节点是否相同,如果相同则直接应用算法2判断pRoot2是否是pRoot1的子树,否则判断pRoot2是否是pRoot1.left的子树,如果是使用算法2,如果不是再判断pRoot2是否是pRoot1.right的子树,如果是使用算法2,如果还不是那就返回False,如果三种情况有一种满足则调用算法2并传入对应的参数。
913

14+
算法2(两个根节点相同的树,如何判断一个是否是另一个的子树):
15+
首先判断Node2是否已经为None,如果是则说明Node2已经遍历完了,返回True
16+
如果Node2不为None则判断Node1是否为None,如果是说明Node2还没为None而Node1却为None了所以返回False
17+
如果Node1和Node2均不为None,递归调用算法2分别判断Node1和Node2的左右子树是否满足子树关系。
1018

1119

1220
## 代码
1321

14-
[这里](../Code/16.py)
22+
```python
23+
24+
def HasSubtree(self, pRoot1, pRoot2):
25+
# write code here
26+
if pRoot1 == None or pRoot2 == None:
27+
return False
28+
flag = False
29+
if pRoot1.val == pRoot2.val:
30+
flag = self.doseNode1HasNode2(pRoot1, pRoot2)
31+
32+
if not flag:
33+
flag = self.HasSubtree(pRoot1.left, pRoot2)
34+
35+
if not flag:
36+
flag = self.HasSubtree(pRoot1.right, pRoot2)
37+
38+
return flag
39+
40+
def doseNode1HasNode2(self, Node1, Node2):
41+
if Node2 == None:
42+
return True
43+
if Node1 == None:
44+
return False
45+
if Node1.val != Node2.val:
46+
return False
47+
48+
return self.doseNode1HasNode2(Node1.left, Node2.left) and self.doseNode1HasNode2(Node1.right, Node2.right)
49+
50+
51+
```
52+
53+
[全部代码](../Code/16.py)

SwordToOffer/Doc/树的镜像.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 树的镜像
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
操作给定的二叉树,将其变换为源二叉树的镜像。
8+
## 输入描述
9+
10+
![image-20190221212341090](https://ws3.sinaimg.cn/large/006tKfTcly1g0ed3jr4ngj30re0h83zc.jpg)
11+
12+
## 解题思路
13+
14+
递归的思想:
15+
16+
```python
17+
def Mirror2(self, root):
18+
# write code here
19+
if not root:
20+
return root
21+
node = root.left
22+
root.left = root.right
23+
root.right = node
24+
self.Mirror2(root.left)
25+
self.Mirror2(root.right)
26+
return root
27+
28+
```
29+
30+
31+
32+
## 代码
33+
34+
[这里](../Code/17.py)

0 commit comments

Comments
 (0)