@@ -52,12 +52,28 @@ def __init__(self, x):
5252# END STATEMENT
5353
5454
55- def _construct_tree(parent: List[Optional[int]], idx: int = 0) -> Optional[TreeNode]:
56- if idx >= len(parent) or parent[idx] is None:
57- return None
58- root = TreeNode(parent[idx])
59- root.left = _construct_tree(parent, idx * 2)
60- root.right = _construct_tree(parent, idx * 2 + 1)
55+ def _construct_tree(parent: List[Optional[int]]) -> Optional[TreeNode]:
56+ from queue import Queue
57+ q: 'Queue[TreeNode]' = Queue()
58+ ptr = 0
59+
60+ def _add_node() -> Optional[TreeNode]:
61+ nonlocal ptr
62+ if ptr >= len(parent):
63+ return None
64+ val = parent[ptr]
65+ ptr += 1
66+ if val is None:
67+ return None
68+ p = TreeNode(val)
69+ q.put(p)
70+ return p
71+
72+ root = _add_node()
73+ while not q.empty():
74+ p = q.get()
75+ p.left = _add_node()
76+ p.right = _add_node()
6177 return root
6278
6379
@@ -170,11 +186,11 @@ def assign(obj_name: str, value: str) -> str:
170186 assign (ret_ans_var , to_val (ex .output , func_sig .return_type )),
171187 assign (ret_name , f"{ instance_name } .{ call (ex .function , args )} " ),
172188 call ("test" , [to_str (f"{ problem .name } - Example { idx } - Interaction { ex_idx } " ),
173- ret_ans_var , ret_name ]) + ";" ,
189+ ret_ans_var , ret_name ]),
174190 ]
175191 statements .extend (stmts )
176192 else :
177- stmt = call (ex .function , args ) + ";"
193+ stmt = call (ex .function , args )
178194 statements .append (stmt )
179195 test_fn = [
180196 f"def test_example_{ idx } ():" ,
@@ -201,7 +217,7 @@ def assign(obj_name: str, value: str) -> str:
201217 stmts = [
202218 assign (ret_ans_var , to_val (example .output , func_sig .return_type )),
203219 assign (ret_name , f"{ instance_name } .{ call (func_sig .name , args )} " ),
204- call ("test" , [to_str (f"{ problem .name } - Example { idx } " ), ret_ans_var , ret_name ]) + ";" ,
220+ call ("test" , [to_str (f"{ problem .name } - Example { idx } " ), ret_ans_var , ret_name ]),
205221 ]
206222 statements .extend (stmts )
207223
0 commit comments