zoukankan      html  css  js  c++  java
  • 栈和队列_leetcode144-二叉树前序遍历非递归

    # Definition for a binary tree node.
    # class TreeNode(object):
    # def __init__(self, x):
    # self.val = x
    # self.left = None
    # self.right = None

    class Solution(object):
    def preorderTraversal(self, root):
    """
    :type root: TreeNode
    :rtype: List[int]
    """

    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))
    if com.node.left:
    stack.append(Command("go",com.node.left))
    stack.append(Command("print",com.node))

    return res
  • 相关阅读:
    web&http协议&django初识
    jQuery
    BOM&DOM
    装饰器
    JavaScript
    模块
    面向对象编程
    函数
    CSS
    HTML
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10547393.html
Copyright © 2011-2022 走看看