mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -170,7 +170,53 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
# 前序遍历-递归-LC144_二叉树的前序遍历
|
||||
class Solution:
|
||||
def preorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
# 保存结果
|
||||
result = []
|
||||
|
||||
def traversal(root: TreeNode):
|
||||
if root == None:
|
||||
return
|
||||
result.append(root.val) # 前序
|
||||
traversal(root.left) # 左
|
||||
traversal(root.right) # 右
|
||||
|
||||
traversal(root)
|
||||
return result
|
||||
|
||||
# 中序遍历-递归-LC94_二叉树的中序遍历
|
||||
class Solution:
|
||||
def inorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
result = []
|
||||
|
||||
def traversal(root: TreeNode):
|
||||
if root == None:
|
||||
return
|
||||
traversal(root.left) # 左
|
||||
result.append(root.val) # 中序
|
||||
traversal(root.right) # 右
|
||||
|
||||
traversal(root)
|
||||
return result
|
||||
|
||||
# 后序遍历-递归-LC145_二叉树的后序遍历
|
||||
class Solution:
|
||||
def postorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
result = []
|
||||
|
||||
def traversal(root: TreeNode):
|
||||
if root == None:
|
||||
return
|
||||
traversal(root.left) # 左
|
||||
traversal(root.right) # 右
|
||||
result.append(root.val) # 后序
|
||||
|
||||
traversal(root)
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user