Skip to content

Commit 2848c58

Browse files
committed
Feb 22
1 parent 54713c9 commit 2848c58

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

2025-02-February-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
| February 19 | [1415. The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/) | Medium | Unsolved |
2626
| February 20 | [1980. Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/) | Medium | Solved |
2727
| February 21 | [1261. Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/) | Medium | Solved |
28-
| February 22 | []() | | |
28+
| February 22 | [1028. Recover a Tree From Preorder Traversal](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/) | Hard | Solved |
2929
| February 23 | []() | | |
3030
| February 24 | []() | | |
3131
| February 25 | []() | | |
@@ -39,4 +39,4 @@
3939
| --- | --- | --- | --- |
4040
| Easy | 6 | 6 | 0 |
4141
| Medium | 14 | 11 | 3 |
42-
| Hard | 0 | 0 | 0 |
42+
| Hard | 1 | 1 | 0 |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from typing import Optional
2+
3+
4+
# Definition for a binary tree node.
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
14+
tree = {-1: TreeNode()} # dummy
15+
depth, value = 0, ''
16+
for ch in traversal + '-':
17+
if ch == '-' and value:
18+
parent = tree[depth - 1]
19+
child = TreeNode(int(value))
20+
if parent.left is None:
21+
parent.left = child
22+
else:
23+
parent.right = child
24+
tree[depth] = child
25+
depth = 1
26+
value = ''
27+
elif ch == '-':
28+
depth += 1
29+
else:
30+
value += ch
31+
return tree[-1].left
32+
33+
34+
def main():
35+
traversal = '1-2--3--4-5--6--7'
36+
root = Solution().recoverFromPreorder(traversal)
37+
assert root.val == 1
38+
assert root.left.val == 2
39+
assert root.right.val == 5
40+
assert root.left.left.val == 3
41+
assert root.left.right.val == 4
42+
assert root.right.left.val == 6
43+
assert root.right.right.val == 7
44+
45+
traversal = '1-2--3---4-5--6---7'
46+
root = Solution().recoverFromPreorder(traversal)
47+
assert root.val == 1
48+
assert root.left.val == 2
49+
assert root.right.val == 5
50+
assert root.left.left.val == 3
51+
assert root.left.left.left.val == 4
52+
assert root.right.left.val == 6
53+
assert root.right.left.left.val == 7
54+
55+
traversal = '1-401--349---90--88'
56+
root = Solution().recoverFromPreorder(traversal)
57+
assert root.val == 1
58+
assert root.left.val == 401
59+
assert root.right is None
60+
assert root.left.left.val == 349
61+
assert root.left.right.val == 88
62+
assert root.left.left.left.val == 90
63+
64+
65+
if __name__ == '__main__':
66+
main()

0 commit comments

Comments
 (0)