-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_p0623_add_one_row_to_tree.py
62 lines (49 loc) · 1.3 KB
/
test_p0623_add_one_row_to_tree.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
# flake8: noqa: F403, F405
from typing import Deque
import pytest
from collections import deque
from leetcode.p0623_add_one_row_to_tree import *
solutions = [
addOneRow,
]
test_cases = [
([[4, 2, 6, 3, 1, 5], 1, 2], [4, 1, 1, 2, None, None, 6, 3, 1, 5]),
([[2, 1, 3], 1, 1], [1, 2, None, 1, 3]),
([[2], 3, 2], [2, 3, 3]),
]
@pytest.mark.timeout(1)
@pytest.mark.parametrize(("args", "expectation"), test_cases)
@pytest.mark.parametrize("solution", solutions)
def test_solution(args, expectation, solution):
a, v, d = args
t = tree(a)
assert array(solution(t, v, d)) == expectation
def tree(a):
root = TreeNode(a[0])
q = deque([root])
ix = 1
while q:
node = q.popleft()
if ix < len(a) and a[ix] is not None:
node.left = TreeNode(a[ix])
q.append(node.left)
ix += 1
if ix < len(a) and a[ix] is not None:
node.right = TreeNode(a[ix])
q.append(node.right)
ix += 1
return root
def array(root):
a = []
q = deque([root])
while q:
node = q.popleft()
if node is not None:
a.append(node.val)
q.append(node.left)
q.append(node.right)
else:
a.append(None)
while a and a[-1] is None:
a.pop()
return a