关注
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)
查看原帖
点赞 评论
相关推荐
03-12 09:42
东北林业大学 科研助理 点赞 评论 收藏
分享
02-27 19:45
青岛农业大学 嵌入式工程师 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 你感受到金三银四了嘛? #
76449次浏览 631人参与
# 你上一次加班是什么时候? #
137376次浏览 758人参与
# 虽然0面试,但今天___,夸夸自己 #
9638次浏览 181人参与
# 2025秋招体验点评 #
99813次浏览 753人参与
# 春招 / 实习投递,你最焦虑的一件事 #
55767次浏览 1068人参与
# 美团笔试 #
699912次浏览 4660人参与
# 米哈游笔试 #
554805次浏览 1101人参与
# AI岗位暴涨12倍,你会转AI赛道吗? #
5578次浏览 97人参与
# 今天你投了哪些公司? #
160223次浏览 2812人参与
# vivo笔试 #
13174次浏览 123人参与
# 金三银四,你的春招进行到哪个阶段了? #
18742次浏览 255人参与
# 27届实习投递记录 #
1048次浏览 24人参与
# 字节7000实习来了,你投了吗? #
4906次浏览 23人参与
# 小米编程考试 #
31977次浏览 151人参与
# 文科生还参加今年的春招吗 #
13764次浏览 100人参与
# 你遇到过哪些神仙同事 #
133821次浏览 762人参与
# AI项目实战 #
7163次浏览 342人参与
# 深信服求职进展汇总 #
258218次浏览 1812人参与
# 蚂蚁集团笔试 #
3956次浏览 22人参与
# 腾讯音乐求职进展汇总 #
157772次浏览 1070人参与