Skip to content

Commit 8771f03

Browse files
committed
feat: add construct binary tree from preorder and inorder traversal solution
1 parent d6819ca commit 8771f03

File tree

1 file changed

+32
-0
lines changed
  • construct-binary-tree-from-preorder-and-inorder-traversal

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List
2+
3+
4+
class Codec:
5+
def encode(self, strs: List[str]) -> str:
6+
"""Encodes a list of strings to a single string."""
7+
encoded = []
8+
9+
for s in strs:
10+
encoded.append(s.replace("/", "//") + "/:")
11+
12+
return "".join(encoded)
13+
14+
def decode(self, s: str) -> List[str]:
15+
"""Decodes a single string to a list of strings."""
16+
decoded = []
17+
current_string = ""
18+
i = 0
19+
20+
while i < len(s):
21+
if s[i : i + 2] == "/:":
22+
decoded.append(current_string)
23+
current_string = ""
24+
i += 2
25+
elif s[i : i + 2] == "//":
26+
current_string += "/"
27+
i += 2
28+
else:
29+
current_string += s[i]
30+
i += 1
31+
32+
return decoded

0 commit comments

Comments
 (0)