-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraversal.py
51 lines (45 loc) · 1.18 KB
/
Traversal.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : Traversal.py
@Time : 2024/09/30 14:43:08
@Author : Zihao Zheng
@Email : zhengzihao718@163.com
'''
class TreeNode():
def __init__(self,val=0,left=None,right=None):
self.valuie=val
self.left=left
self.right=right
return
class Solution():
def preorder(self,root:TreeNode):
res=[]
def dfs(node):
if node is None:
return
res.append(node.val)
dfs(node.left)
dfs(node.right)
dfs(root)
return res
def midorder(self,root:TreeNode):
res=[]
def dfs(node):
if node is None:
return
dfs(node.left)
res.append(node.val)
dfs(node.right)
dfs(root)
return res
def postorder(self,root:TreeNode):
res=[]
def dfs(node):
if node is None:
return
dfs(node.left)
dfs(node.right)
res.append(node.val)
dfs(root)
return res