def postorder(root): if root is None: return [] stack = [] result = [] visited = set() stack.append(root) while stack: curr = stack[-1] if (curr.left is None or curr.left in visited) and (curr.right is None or curr.right in visited): stack.pop() visited.add(curr) result.append(curr.value) else: if curr.right is not None and curr.right not in visited: stack.append(curr.right) if curr.left is not None and curr.left not in visited: stack.append(curr.left) for i in result: print(i,end=",") return result
时间: 2024-02-10 20:19:08 浏览: 29
这是一段二叉树后序遍历的代码,它采用了迭代的方式实现。具体实现方式是通过使用一个栈来模拟递归过程,先将根节点压入栈中,然后进行循环。每次循环时,取出栈顶元素,如果它的左右子节点都已经被访问过了,就将它弹出栈,并将它的值加入到结果列表中,否则将它的右子节点和左子节点依次压入栈中。最后输出结果列表即可。
你有什么其他问题吗?
相关问题
Write a function to print the input tree by postorder traversal, preorder traversal, inorder traversal.
好的,这个问题涉及二叉树的遍历方法。以下是对应的函数实现。
```python
# 定义树的节点类
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# 后序遍历: 左子树 -> 右子树 -> 根节点
def postorderTraversal(root):
if not root:
return []
res = []
stack = [(root, False)]
while stack:
node, visited = stack.pop()
if visited:
res.append(node.val)
else:
stack.append((node, True))
if node.right:
stack.append((node.right, False))
if node.left:
stack.append((node.left, False))
return res[::-1]
# 先序遍历: 根节点 -> 左子树 -> 右子树
def preorderTraversal(root):
if not root:
return []
res = []
stack = [root]
while stack:
node = stack.pop()
res.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return res
# 中序遍历: 左子树 -> 根节点 -> 右子树
def inorderTraversal(root):
if not root:
return []
res = []
stack = []
node = root
while stack or node:
if node:
stack.append(node)
node = node.left
else:
node = stack.pop()
res.append(node.val)
node = node.right
return res
```
这里我们使用了栈来实现遍历。函数接收一个根节点,并返回对应遍历方式得到的节点值的列表。
希望这个回答能够帮助到您!
以二叉链表作为二叉树的存储结构,编写以下算法: 1.统计二叉树的叶结点个数。 2.判别两棵树是否相等。 3.交换二叉树每个结点的左孩子和右孩子。 4.设计二叉树的双序遍历算法
1. 统计二叉树的叶结点个数
```python
def count_leaves(root):
if not root:
return 0
if not root.left and not root.right: # 如果该结点没有左孩子和右孩子,则为叶子结点
return 1
return count_leaves(root.left) + count_leaves(root.right)
```
2. 判别两棵树是否相等
```python
def is_equal(root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
if root1.val != root2.val:
return False
return is_equal(root1.left, root2.left) and is_equal(root1.right, root2.right)
```
3. 交换二叉树每个结点的左孩子和右孩子
```python
def swap_children(root):
if not root:
return
root.left, root.right = root.right, root.left
swap_children(root.left)
swap_children(root.right)
```
4. 设计二叉树的双序遍历算法
```python
def double_order_traversal(root):
if not root:
return []
stack = [root]
res_preorder, res_postorder = [], []
while stack:
node = stack.pop()
res_preorder.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
stack = [root]
visited = set()
while stack:
node = stack[-1]
if node.left and node.left not in visited:
stack.append(node.left)
elif node.right and node.right not in visited:
stack.append(node.right)
else:
res_postorder.append(node.val)
visited.add(node)
stack.pop()
return res_preorder, res_postorder
```
其中,res_preorder 为先序遍历结果,res_postorder 为后序遍历结果。