关注
Python解法 利用层序和中序递归创建二叉树,然后递归打印叶子节点,morris 前序后序遍历二叉树 # -*- coding=utf-8 -*-
class TreeNode():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Solution():
def recreate_tree(self, level, mid_order):
if not level or not mid_order:
return None
root = TreeNode(level[0])
root_index = mid_order.index(level[0])
left_mid_order = mid_order[:root_index]
right_mid_order = mid_order[root_index+1:]
left_level = [item for item in level if item in left_mid_order]
right_level = [item for item in level if item in right_mid_order]
root.left = self.recreate_tree(left_level, left_mid_order)
root.right = self.recreate_tree(right_level, right_mid_order)
return root
def morris_pre_traverse(self, root):
if root is None:
return None
cur = root
most_right = None
while cur:
most_right = cur.left
if most_right:
while most_right.right and most_right.right != cur:
most_right = most_right.right
if most_right.right is None:
most_right.right = cur
# 第一次到达即打印
print(cur.val, end=' ')
cur = cur.left
continue
else:
most_right.right = None
else:
# 没有左子树的只会到达一次
print(cur.val, end=' ')
cur = cur.right
print('')
def print_leaf(self, root):
if not root.left and not root.right:
print(root.val, end=' ')
return
if root.left:
self.print_leaf(root.left)
if root.right:
self.print_leaf(root.right)
def morris_post_traverse(self, root):
if root is None:
return None
cur = root
most_right = None
while cur:
most_right = cur.left
if most_right:
while most_right.right and most_right.right != cur:
most_right = most_right.right
if most_right.right is None:
most_right.right = cur
cur = cur.left
continue
else:
most_right.right = None
# 在第二次到达时逆序打印cur节点的左子树的右边界
self.print_edge(cur.left)
cur = cur.right
# 逆序打印整个二叉树的右边界
self.print_edge(root)
print('')
def print_edge(self, node):
tail = self.reverse_edge(node)
temp = tail
while temp:
print(temp.val, end=' ')
temp = temp.right
self.reverse_edge(tail)
def reverse_edge(self, node):
pre = None
while node:
next_node = node.right
node.right = pre
pre = node
node = next_node
return pre
if __name__ == "__main__":
level = list(map(int, input().split()))
mid_order = list(map(int, input().split()))
ex = Solution()
root = ex.recreate_tree(level, mid_order)
ex.print_leaf(root)
print('')
ex.morris_pre_traverse(root)
ex.morris_post_traverse(root)
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛客2025仙途报告 #
4288次浏览 126人参与
# 礼物开箱Plog #
1533次浏览 69人参与
# 2025年终总结 #
177263次浏览 2997人参与
# 工作两年,想和老板谈涨薪怎么说 #
38785次浏览 175人参与
# 你面试体验感最差/最好的公司 #
22123次浏览 361人参与
# 秋招落幕,你是He or Be #
15504次浏览 286人参与
# 一人说一个提前实习的好处 #
13927次浏览 227人参与
# 考公VS就业,你怎么选? #
88100次浏览 497人参与
# 今年你最想重开的一场面试是? #
5632次浏览 74人参与
# 离家近房租贵VS离家远但房租低,怎么选 #
13684次浏览 130人参与
# 重来一次,你会对开始求职的自己说 #
6886次浏览 173人参与
# 找工作,行业重要还是岗位重要? #
85780次浏览 1699人参与
# 实习没事做是福还是祸? #
18746次浏览 272人参与
# 机械制造秋招总结 #
97352次浏览 878人参与
# 职场新人体验 #
156928次浏览 1121人参与
# 工作中听到最受打击的一句话 #
8169次浏览 129人参与
# 团建是“福利”还是是 “渡劫” #
8087次浏览 160人参与
# 反问环节如何提问 #
126453次浏览 2669人参与
# 移动求职进展汇总 #
17916次浏览 143人参与
# 比亚迪线下宣讲会 #
17177次浏览 50人参与
