Skip to content

Commit e124be7

Browse files
fix and add
增加了二叉树的遍历以及优化部分代码
1 parent 00576fd commit e124be7

4 files changed

Lines changed: 153 additions & 64 deletions

File tree

.idea/workspace.xml

Lines changed: 53 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Target Offer/二维数组查找.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ def Find(self, array, target):
2727
colnum = len(array[0])
2828

2929
# 判断非法输入
30+
# 可以换成 isinstance(target, (int, float)) 进行判断
3031
if type(target) == float and type(array[0][0]) == int:
3132
target = int(target)
3233
elif type(target) == int and type(array[0][0]) == float:
3334
target = float(int)
34-
elif type(target) != type(array[0][0]):
35+
elif type(target) != type(array[0][0]): # 浮点数的相等判断问题需要特别注意, 一般都是判断两个数的差值是否小于一个特别小的数。这里不展开分析。
3536
return False
3637

3738
i = colnum - 1

Target Offer/替换空格.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,35 @@ def replaceSpace1(self, s):
2020
return tempstr
2121

2222
# 简单代码替换
23+
# 在Python中str类型是不可变的类型, 使用replace语句会生成一个新的str, 原始的s还是带空格的str变量
2324
def replaceSpace2(self, s):
2425
if type(s) != str:
2526
return
2627
return s.replace(' ', '%20')
2728

2829
# 书中给的思路
30+
# 判断输入类型的时候,isinstance必须首先判断,因为如果输入为integer的话,没有len,就会直接报错
2931
def replaceSpace3(self, s):
30-
if type(s) != str:
31-
return
32-
if len(s) <= 0:
33-
return
32+
if not isinstance(s,str) or len(s) <= 0 or s == None:
33+
return ""
3434
spaceNum = 0
3535
for i in s:
3636
if i == " ":
3737
spaceNum += 1
3838

3939
newStrLen = len(s) + spaceNum * 2
4040
newStr = newStrLen * [None]
41-
indexOfOriginal = len(s) - 1
42-
indexOfNew = newStrLen - 1
41+
indexOfOriginal, indexOfNew = len(s) - 1, newStrLen - 1
4342
while indexOfNew >= 0 and indexOfNew >= indexOfOriginal:
4443
if s[indexOfOriginal] == ' ':
45-
newStr[indexOfNew] = '0'
46-
newStr[indexOfNew-1] = '2'
47-
newStr[indexOfNew-2] = '%'
44+
newStr[indexOfNew-2:indexOfNew+1] = ['%', '2', '0']
4845
indexOfNew -= 3
4946
indexOfOriginal -= 1
5047
else:
5148
newStr[indexOfNew] = s[indexOfOriginal]
5249
indexOfNew -= 1
5350
indexOfOriginal -= 1
54-
changedStr = ''
55-
for c in newStr:
56-
changedStr += c
57-
return changedStr
51+
return "".join(newStr)
5852

5953

6054
s = 'we are happy'
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -*- coding:UTF-8 -*-
2+
'''
3+
利用递归以及非递归的方式实现二叉搜索树的前序遍历、中序遍历和后序遍历
4+
'''
5+
6+
class TreeNode:
7+
def __init__(self, x):
8+
self.val = x
9+
self.left = None
10+
self.right = None
11+
12+
class Tranversal:
13+
# preorder without recursion
14+
def preOrder(self, root):
15+
if root == None:
16+
return None
17+
pNode, treeStack = root, []
18+
while pNode or len(treeStack) > 0:
19+
while pNode:
20+
print(pNode.val)
21+
treeStack.append(pNode)
22+
pNode = pNode.left
23+
if len(treeStack) > 0:
24+
pNode = treeStack.pop()
25+
pNode = pNode.right
26+
# preorder with recursion
27+
def preOrderRec(self, root):
28+
if root != None:
29+
print(root.val)
30+
self.preOrderRec(root.left)
31+
self.preOrderRec(root.right)
32+
# inorder without recursion
33+
def inOrder(self, root):
34+
if root == None:
35+
return
36+
pNode, treeStack = root, []
37+
while pNode or len(treeStack) > 0:
38+
while pNode:
39+
treeStack.append(pNode)
40+
pNode = pNode.left
41+
if len(treeStack) > 0:
42+
pNode = treeStack.pop()
43+
print(pNode.val)
44+
pNode = pNode.right
45+
# inorder with recursion
46+
def inOrderRec(self, root):
47+
if root != None:
48+
self.inOrderRec(root.left)
49+
print(root.val)
50+
self.inOrderRec(root.right)
51+
# postorder without recursion
52+
def postOrder(self, root):
53+
if root == None:
54+
return
55+
cur, pre, treeStack = root, None, [] # cur:current Node, pre: pre visited Node
56+
treeStack.append(root)
57+
while len(treeStack) > 0:
58+
cur = treeStack[-1]
59+
# current node doesn't have child nodes or child nodes have been visited
60+
if (cur.left == None and cur.right == None) or (pre != None and (pre == cur.left or pre == cur.right)):
61+
print(cur.val)
62+
pre = treeStack.pop()
63+
else:
64+
if cur.right != None:
65+
treeStack.append(cur.right)
66+
if cur.left != None:
67+
treeStack.append(cur.left)
68+
# postorder with cursion
69+
def postOrderRec(self, root):
70+
if root:
71+
self.postOrderRec(root.left)
72+
self.postOrderRec(root.right)
73+
print(root.val)
74+
75+
pNode1 = TreeNode(10)
76+
pNode2 = TreeNode(6)
77+
pNode3 = TreeNode(14)
78+
pNode4 = TreeNode(4)
79+
pNode5 = TreeNode(8)
80+
pNode6 = TreeNode(12)
81+
pNode7 = TreeNode(16)
82+
83+
pNode1.left = pNode2
84+
pNode1.right = pNode3
85+
pNode2.left = pNode4
86+
pNode2.right = pNode5
87+
pNode3.left = pNode6
88+
pNode3.right = pNode7
89+
90+
S = Tranversal()
91+
S.postOrder(pNode1)

0 commit comments

Comments
 (0)