python 实现二叉树的(先|中|后|层次)序遍历,递归和非递归实现
二叉树的遍历
先序遍历
# 递归
def pre_through(head):
if head is None:
return
print(head.val)
pre_through(head.left)
pre_through(head.right)
# 非递归
def pre_through(head):
if head is None:
return
s = list()
s.append(head)
while head:
head = s.pop(0)
pr
原创
2020-10-04 16:06:08 ·
124 阅读 ·
0 评论