144. 二叉树的前序、中序、后序遍历 leetcode

本文参考自: 原文地址

给定一个二叉树,返回它的 前序 遍历。

 示例:

输入: [1,null,2,3]  
   1
    \
     2
    /
   3 

输出: [1,2,3]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

模拟栈的操作。对于这个问题,实际上在计算机中是这样处理的。我们首先将访问node1的right访问node1的left打印node.val压入栈中。

stack : go-1-R   go-1-L   cout
  • 1

然后弹出cout,我们就打印了node1.val。接着访问node1的left,我们发现node1的left是空,那么什么也不做。我们接着访问node1的right,同样的对于node1.right,我们要推入这样的三个指令go-2-Rgo-2-Lcout,就变成了下面这个样子。

stack : go-2-R   go-2-L   cout
  • 1

然后弹出cout,我们就打印了node2.val。接着访问node2的left,我们要推入这样的三个指令go-3-Rgo-3-Lcout,就变成了下面这个样子。

stack : go-2-R   go-3-R   go-3-L   cout
  • 1

然后弹出cout,我们就打印了node3.val。接着访问node3的left,我们发现为空,那们我们什么都不做。接着访问node3的right,我们发现为空,那么我们什么也不做。接着访问node2的right,我们发现为空,我们同样什么也不做。这个时候我们发现栈为空,那么我们就结束了所有操作。我们根据上述思路,可以很容易地写出下面的代码:

class Solution:  
    def preorderTraversal(self, root):
        """ :type root: TreeNode :rtype: List[int] """
        result = list()
        if root == None:
            return result

        stack = list()
        stack.append(root)
        while stack:
            top = stack.pop()
            if top.right != None:
                stack.append(top.right)
            if top.left != None:
                stack.append(top.left)
            result.append(top.val)        return result

在中序遍历时:

  • 如果root不为空,我们一直压入root,并且更新root=root.left,这样left会一直压栈操作。
  • 当我们发现left为空的时候,我们就要将结果压入result,接着访问right,然后回到第一步
  • 直到len(stack)==0,我们就结束了。

class Solution:  
    def inorderTraversal(self, root):
        """ :type root: TreeNode :rtype: List[int] """
        result = list()
        if root == None:
            return result

        stack = list()
        while stack or root:
            if root != None:
                stack.append(root)
                root = root.left
            else:
                root = stack.pop()
                result.append(root.val)
                root = root.right

        return result

在后序遍历代码:

class Solution:
    def postorderTraversal(self, root):
        """ :type root: TreeNode :rtype: List[int] """
        result = list()
        if root == None:
            return result

        stack = list()
        stack.append(root)
        while len(stack) != 0:
            top = stack.pop()
            if top.left != None:
                stack.append(top.left)
            if top.right != None:
                stack.append(top.right)

            result.insert(0, top.val)

        return result

当然我们也可以参考中序遍历的思路,想出这样的解法

  • 如果root不为空,我们将cout操作压栈,同时我们一同压入root,并且更新root=root.right,这样right会和cout操作一直做压栈操作。
  • 当我们发现right为空的时候,我们接着访问栈顶node的left,接着回到第一步。
  • 直到len(stack)==0,我们就结束了。
class Solution:
    def postorderTraversal(self, root):
        """ :type root: TreeNode :rtype: List[int] """
        result = list()
        if root == None:
            return result

        stack = list()
        while stack or root:
            if root:
                stack.append(root)
                result.insert(0, root.val)
                root = root.right
            else:
                node = stack.pop()
                root = node.left

        return result

猜你喜欢

转载自blog.csdn.net/qq_39706019/article/details/81430322