-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlctest.py
150 lines (120 loc) · 3.93 KB
/
lctest.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copy test code from leetcode playground
# https://leetcode-cn.com/playground/new/empty/
# --------------------------------------------
# TreeNode
# --------------------------------------------
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def stringToTreeNode(input):
input = input.strip()
input = input[1:-1]
if not input:
return None
inputValues = [s.strip() for s in input.split(',')]
root = TreeNode(int(inputValues[0]))
nodeQueue = [root]
front = 0
index = 1
while index < len(inputValues):
node = nodeQueue[front]
front = front + 1
item = inputValues[index]
index = index + 1
if item != "null":
leftNumber = int(item)
node.left = TreeNode(leftNumber)
nodeQueue.append(node.left)
if index >= len(inputValues):
break
item = inputValues[index]
index = index + 1
if item != "null":
rightNumber = int(item)
node.right = TreeNode(rightNumber)
nodeQueue.append(node.right)
return root
def treeNodeToString(root):
if not root:
return "[]"
output = ""
queue = [root]
current = 0
while current != len(queue):
node = queue[current]
current = current + 1
if not node:
output += "null, "
continue
output += str(node.val) + ", "
queue.append(node.left)
queue.append(node.right)
return "[" + output[:-2] + "]"
def treeNodeToCompactString(root):
if root is None:
return "[]"
array = []
queue = [root]
current = 0
while current != len(queue):
node = queue[current]
current += 1
if node is None:
array.append("null")
continue
array.append(str(node.val))
queue.append(node.left)
queue.append(node.right)
nulls = 0
for val in reversed(array):
if val == "null":
nulls += 1
else:
break
if nulls > 0:
array = array[:-1*nulls]
return "[" + ",".join(array) + "]"
class TreeNodeHelper:
# node1和node2互换位置,思路:首先生成一个新节点temp1替换掉node1的位置
# 再生成一个新节点temp2替换掉node2的位置,此时node1和node2
def swap(self, node1, node2):
node1, temp1 = self.replace(node1)
node2, temp2 = self.replace(node2)
self.replace(temp1, node2)
self.replace(temp2, node1)
# 将root节点替换为node,如果node为空,就创建一个新节点代替。
# 这里的替换是指把root与父节点和子节点的关系,都转移给node。
# 所以还需要一个字典来预先保存好root与父节点的关系。
def replace(self, root, node=None):
if node is None:
node = TreeNode(root.val)
# assert node.left is None
# assert node.right is None
# assert node not in self.cache
# 转移子节点
node.left = root.left
node.right = root.right
# 转移父节点
if root in self.cache:
super_root, relationship = self.cache[root]
if relationship == 1:
super_root.left = node
elif relationship == 2:
super_root.right == node
else:
pass # assert False
# 更新字典中的父子关系
del self.cache[root]
self.cache[node] = (super_root, relationship)
# 更新字典中的孩子关系
if root.left:
self.cache[root.left] = (node, 1)
if root.right:
self.cache[root.right] = (node, 2)
# 把交换后的root与原树脱离关系
root.left = None
root.right = None
# 返回交换后的两个节点
return root, node