前序遍历:根、左、右
中序遍历:左、根、右
后序遍历:左、右、根
代码:
# 前序遍历
def preorder(tree):
if tree:
print(tree.getRootVal())
preorder(tree.getLeftChildren())
preorder(tree.getRightChildren())
# 中序遍历
def inorder(tree):
if tree:
inorder(tree.getLeftChildren())
print(tree.getRootVal())
preorder(tree.getRightChildren())
# 后序遍历
def postorder(tree):
if tree:
postorder(tree.getLeftChildren())
postorder(tree.getRightChildren())
print(tree.getRootVal())
网友评论