-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinNode.py
120 lines (101 loc) · 2.05 KB
/
BinNode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
1
/ \
2 3
/ \ \
4 5 6
- 层次遍历顺序:[1 2 3 4 5 6]
- 前序遍历顺序:[1 2 4 5 3 6]
- 中序遍历顺序:[4 2 5 1 3 6]
- 后序遍历顺序:[4 5 2 6 3 1]
"""
class TreeNode():
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
"""
1
/ \
2 3
/ \ \
4 5 6
- 前序遍历顺序:[1 2 4 5 3 6]
"""
# 非递归
def preOrder1(root):
if not root:
return
myStack = []
node = root
while node or myStack:
while node:
print(node.val)
myStack.append(node)
node = node.left
node = myStack.pop()
node = node.right
def preOrder2(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result = []
stack = []
stack.append(root)
while len(stack) != 0:
node = stack.pop()
if node is None:
continue
result.append(node.val)
stack.append(node.right)
stack.append(node.left)
return result
"""
1
/ \
2 3
/ \ \
4 5 6
- 中序遍历顺序:[4 2 5 1 3 6]
"""
def inOrder(root): # 中序
res = []
stack = []
if root is None:
return res
cur = root
while len(stack) != 0 or cur is not None:
while cur is not None:
stack.append(cur)
cur = cur.left
node = stack.pop()
res.append(node.val)
cur = node.right
return res
"""
1
/ \
2 3
/ \ \
4 5 6
- 后序遍历顺序:[4 5 2 6 3 1]
"""
def postOrder(root):
result = []
stack = []
stack.append(root)
while len(stack) != 0:
node = stack.pop()
if node is None:
continue
result.append(node.val)
stack.append(node.left)
stack.append(node.right)
return result[::-1]
if __name__ == "__main__":
t = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)),
TreeNode(3, None, TreeNode(6)))
print(preOrder2(t))
print(inOrder(t))
print(postOrder(t))