首页 > 其他 > 详细

栈和队列_leetcode94

时间:2019-03-17 16:23:46      阅读:178      评论:0      收藏:0      [点我收藏+]
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype
"""

res = []
self.order(root, res)
return res


def order(self, root, res):
if root:

self.order(root.left, res)
res.append(root.val)
self.order(root.right, res)


class Solution2(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

栈和队列_leetcode94

原文:https://www.cnblogs.com/lux-ace/p/10547319.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!