美文网首页
二叉树的前中后序遍历(1)

二叉树的前中后序遍历(1)

作者: 时间煮菜 | 来源:发表于2020-03-26 16:52 被阅读0次

python![1572051603644]


前序遍历

# encoding:utf-8
# 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]
        """
        stack = []  # 只用来存放根节点值
        cur = root
        res = []    # 用来存放所有结点值
        if not cur:
            return []
        while stack or cur:
            while cur:
                stack.append(cur)
                res.append(cur.val)
                cur = cur.left
            node = stack.pop()
            cur = node.right
        return res

    def preorderTraversal1(self, root):  # 递归
        res = []
        cur = root
        stack = []
        while stack or cur:
            if not cur:
                cur = stack.pop()
                cur = cur.right
            else:
                stack.append(cur)
                res.append(cur.val)
                cur = cur.left
        return res


if __name__ == "__main__":
    root = TreeNode("A")
    h1 = TreeNode("B")
    h2 = TreeNode("C")
    h3 = TreeNode("D")
    h4 = TreeNode("E")
    h5 = TreeNode("F")
    h6 = TreeNode("G")
    root.left = h1
    root.right = h2
    h1.left = h3
    h1.right = h4
    h2.left = h5
    h2.right = h6
    s = Solution()
    print s.preorderTraversal1(root)
    print s.preorderTraversal(root)

中序遍历

# encoding:utf-8
# 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 inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        stack = []
        cur = root
        res = []
        while stack or cur:
            while cur:
                stack.append(cur)
                cur = cur.left
            node = stack.pop()
            res.append(node.val)
            cur = node.right
        return res


if __name__ == "__main__":
    root = TreeNode("A")
    h1 = TreeNode("B")
    h2 = TreeNode("C")
    h3 = TreeNode("D")
    h4 = TreeNode("E")
    h5 = TreeNode("F")
    h6 = TreeNode("G")
    root.left = h1
    root.right = h2
    h1.left = h3
    h1.right = h4
    h2.left = h5
    h2.right = h6
    s = Solution()
    print s.inorderTraversal(root)

后序遍历

# encoding:utf-8
# 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 postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        stack = []  # 只用来存放根节点值
        cur = root
        res = []    # 用来存放所有结点值
        last = None
        if not cur:
            return []
        while stack or cur:
            while cur:
                stack.append(cur)
                cur = cur.left
            node = stack[-1]
            if node.right == None or node.right == last:
                node = stack.pop()
                last = node
                res.append(node.val)
            else:
                cur = node.right
        return res




if __name__ == "__main__":
    root = TreeNode("A")
    h1 = TreeNode("B")
    h2 = TreeNode("C")
    h3 = TreeNode("D")
    h4 = TreeNode("E")
    h5 = TreeNode("F")
    h6 = TreeNode("G")
    root.left = h1
    root.right = h2
    h1.left = h3
    h1.right = h4
    h2.left = h5
    h2.right = h6
    s = Solution()
    print s.postorderTraversal(root)

相关文章

  • 07-13:二叉树review1

    二叉树review1: 1、二叉树结构 1)二叉树的遍历 0)递归/迭代实现 前/中/后序遍历 递归 迭代 层次遍...

  • 2018-09-07

    二叉树的前中后序遍历 二叉树由左子树、右子树和根组成(L, R,D) 前,中,后序遍历是针对根节点来说的。DLR ...

  • 二叉树的遍历方式

    二叉树的遍历方式有多种,前序遍历,中序遍历,后序遍历,层序遍历,在这里来介绍一下前、中、后序遍历。 前序遍历:根左...

  • POJ 2255 Tree Recovery(根据前中序遍历,重

    题意:给出二叉树的前序遍历和中序遍历,求后序遍历。 NO.1:无需重建二叉树,可直接求出后序遍历结果。 NO.2 ...

  • 递归调用中的递归序

    从刚开始接触递归,到接触二叉树递归遍历,简单几行代码就能实现前中后序遍历,而且,前中后序遍历的代码基本一致,觉得好...

  • 数据结构与算法二叉树的遍历与线索二叉树以及森林

    1.二叉树的遍历先序遍历、中序遍历、后序遍历 2.层次遍历利用队列实现 3.由遍历序列构成二叉树先序、后序可以与众...

  • leetcode 144 145 94

    二叉树遍历 前序遍历 中序遍历 后序遍历

  • 二叉树的遍历与创建

    二叉树的遍历 分为:前序,中序,后序,层序。 前/中/后序,是根据跟节点遍历的前后顺序来定义的。 前序遍历 从根节...

  • goLang 二叉树遍历(递归 非递归 前序遍历 中序遍历 后序

    goLang 二叉树遍历(递归 非递归 前序遍历 中序遍历 后序遍历 层序遍历) 前序遍历 中序遍历 后序遍历 代...

  • 1. 二叉树的遍历 二叉树的遍历可以有三种 : 先序、 中序、 后序遍历 。先序是根左右 ,中序是左根右 ,后序是...

网友评论

      本文标题:二叉树的前中后序遍历(1)

      本文链接:https://www.haomeiwen.com/subject/oficuhtx.html