-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
99.py
63 lines (57 loc) · 2.05 KB
/
99.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
__________________________________________________________________________________________________
sample 56 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def recoverTree(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
nodes = []
vals = []
self.helper(root, nodes, vals)
vals.sort()
l = len(vals)
for i in range(l):
if nodes[i].val != vals[i]:
nodes[i].val = vals[i]
def helper(self, root: TreeNode, nodes, vals) -> None:
if root != None:
self.helper(root.left, nodes, vals)
nodes.append(root)
vals.append(root.val)
self.helper(root.right, nodes, vals)
__________________________________________________________________________________________________
sample 13160 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def recoverTree(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
stack = []
last_node, node_a, node_b = None, None, None
while stack or root:
if root:
stack.append(root)
root = root.left
else:
root = stack.pop()
if last_node is not None and root.val < last_node.val:
if node_a is None:
node_a = last_node
node_b = root
last_node = root
root = root.right
node_a.val, node_b.val = node_b.val, node_a.val
return root
__________________________________________________________________________________________________