Skip to content

Commit 6e3664b

Browse files
committed
Fix incorrect code for constructing trees
- The original boilerplate code for constructin binary trees from Leetcode's strange syntax was wrong. Now it's fixed. - The generated Python code also contained unnecessary semicolons. Now it's fixed as well.
1 parent 08b054e commit 6e3664b

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

lchelper/codegen/cpp.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,27 @@ def extra_files(self) -> Dict[str, str]:
125125
126126
const int NONE = INT_MIN;
127127
128-
TreeNode *_construct_tree(const vector<int> &parent, int idx = 0) {
129-
if (idx >= parent.size() || parent[idx] == NONE) return NULL;
130-
TreeNode *root = new TreeNode(parent[idx]);
131-
root->left = _construct_tree(parent, idx * 2 + 1);
132-
root->right = _construct_tree(parent, idx * 2 + 2);
128+
TreeNode *_construct_tree(const vector<int> &parent) {
129+
queue<TreeNode *> q;
130+
int ptr = 0;
131+
132+
auto _add_node = [&]() -> TreeNode * {
133+
if (ptr >= parent.size()) return nullptr;
134+
int val = parent[ptr++];
135+
if (val == NONE) return nullptr;
136+
auto *p = new TreeNode(val);
137+
q.push(p);
138+
return p;
139+
};
140+
141+
TreeNode *root = _add_node();
142+
while (!q.empty()) {
143+
if (ptr >= parent.size()) break;
144+
TreeNode *p = q.front();
145+
q.pop();
146+
p->left = _add_node();
147+
p->right = _add_node();
148+
}
133149
return root;
134150
}
135151

lchelper/codegen/python.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)