class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
#二叉树非递归前序序遍历
def pre_order(self, root):
if not root:
return None
node_list = []
p = root
res = []
while p or len(node_list):
# 遍历到左子树最下边的叶子节点,并保存遍历过程中的节点
while p:
node_list.append(p)
res.append(p.val)
p = p.left
# 开始出栈
if len(node_list):
p = node_list[-1]
node_list.pop()
p = p.right
return res
# 二叉树非递归中序遍历
def middle_order(self, root):
node_list = []
p = root
res = []
while p or len(node_list) != 0:
# 遍历到左子树最下边的叶子节点,并保存遍历过程中的节点
while p: