mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #187 from charon2121/master
更新 144.二叉树的前序遍历 python3版本
This commit is contained in:
@ -160,6 +160,68 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
# 前序遍历-迭代-LC144_二叉树的前序遍历
|
||||||
|
class Solution:
|
||||||
|
def preorderTraversal(self, root: TreeNode) -> List[int]:
|
||||||
|
# 根结点为空则返回空列表
|
||||||
|
if not root:
|
||||||
|
return []
|
||||||
|
stack = [root]
|
||||||
|
result = []
|
||||||
|
while stack:
|
||||||
|
node = stack.pop()
|
||||||
|
# 中结点先处理
|
||||||
|
result.append(node.val)
|
||||||
|
# 右孩子先入栈
|
||||||
|
if node.right:
|
||||||
|
stack.append(node.right)
|
||||||
|
# 左孩子后入栈
|
||||||
|
if node.left:
|
||||||
|
stack.append(node.left)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# 中序遍历-迭代-LC94_二叉树的中序遍历
|
||||||
|
class Solution:
|
||||||
|
def inorderTraversal(self, root: TreeNode) -> List[int]:
|
||||||
|
if not root:
|
||||||
|
return []
|
||||||
|
stack = [] # 不能提前将root结点加入stack中
|
||||||
|
result = []
|
||||||
|
cur = root
|
||||||
|
while cur or stack:
|
||||||
|
# 先迭代访问最底层的左子树结点
|
||||||
|
if cur:
|
||||||
|
stack.append(cur)
|
||||||
|
cur = cur.left
|
||||||
|
# 到达最左结点后处理栈顶结点
|
||||||
|
else:
|
||||||
|
cur = stack.pop()
|
||||||
|
result.append(cur.val)
|
||||||
|
# 取栈顶元素右结点
|
||||||
|
cur = cur.right
|
||||||
|
return result
|
||||||
|
|
||||||
|
# 后序遍历-迭代-LC145_二叉树的后序遍历
|
||||||
|
class Solution:
|
||||||
|
def postorderTraversal(self, root: TreeNode) -> List[int]:
|
||||||
|
if not root:
|
||||||
|
return []
|
||||||
|
stack = [root]
|
||||||
|
result = []
|
||||||
|
while stack:
|
||||||
|
node = stack.pop()
|
||||||
|
# 中结点先处理
|
||||||
|
result.append(node.val)
|
||||||
|
# 左孩子先入栈
|
||||||
|
if node.left:
|
||||||
|
stack.append(node.left)
|
||||||
|
# 右孩子后入栈
|
||||||
|
if node.right:
|
||||||
|
stack.append(node.right)
|
||||||
|
# 将最终的数组翻转
|
||||||
|
return result[::-1]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user