栈和队列_leetcode94-二叉树的非递归遍历

class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype
"""

class Command(object):
def __init__(self,com,node):
self.com = com
self.node = node


res = []
stack = []

if not root:
return res
stack.append(Command("go",root))

while stack:
com = stack.pop()

if com.com == "print":
res.append(com.node.val)
else:
if com.node.right:
stack.append(Command("go",com.node.right))
stack.append(Command("print", com.node))
if com.node.left:
stack.append(Command("go",com.node.left))


return res
原文地址:https://www.cnblogs.com/lux-ace/p/10547324.html