-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbt_preorder.py
More file actions
36 lines (25 loc) · 742 Bytes
/
bt_preorder.py
File metadata and controls
36 lines (25 loc) · 742 Bytes
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
from pathlib import Path
import sys
sys.path.append(str(Path(__file__).resolve().parent.parent))
from binary_tree import create_binary_tree
class Solution:
def preorderTraversal(self, root):
if root is None:
return []
result = []
def preorder(node):
if node is None:
return []
result.append(node.val)
preorder(node.left)
preorder(node.right)
preorder(root)
return result
if __name__ == "__main__":
solution = Solution()
root = [1, None, 2, 3]
bt = create_binary_tree(root)
result = solution.preorderTraversal(bt)
assert result == [1, 2, 3]
print("Test Case 1 Passed!")
print(result)