zoukankan      html  css  js  c++  java
  • 二叉树的前序、中序、后序遍历的递归和非递归实现

    一、二叉树的前序遍历LeetCode 144

    递归实现:

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def preorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root:
                return []
            return [root.val] +self.preorderTraversal(root.left)+self.preorderTraversal(root.right)

    非递归实现:

    class Solution:
        def preorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res=[]
            stack=[root]
            while stack:
                node=stack.pop()
                if node:
                    res.append(node.val)
                    stack.append(node.right)
                    stack.append(node.left)
            return res 

    二、二叉树的中序遍历LeetCode 94

    递归实现:

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def inorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root:
                return []
            return self.inorderTraversal(root.left)+[root.val]+self.inorderTraversal(root.right)

    非递归实现:

    class Solution:
        def inorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res,stack=[],[]
            while True:
                while root:
                    stack.append(root)
                    root=root.left
                if not stack:
                    return res
                node=stack.pop()
                res.append(node.val)
                root=node.right
            return res

    三、二叉树的后序遍历(LeetCode 145) 

    递归实现:

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def postorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """        
            if not root:
                return []
            return self.postorderTraversal(root.left)+self.postorderTraversal(root.right)+[root.val]

    非递归实现:

    class Solution:
        def postorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res=[]
            stack=[root]
            while stack:
                node=stack.pop()
                if node:
                    res.append(node.val)
                    stack.append(node.left)
                    stack.append(node.right)
            return res[::-1]
  • 相关阅读:
    含有tuple的list按照tuple中的某一位进行排序
    python
    Python代码追踪(类似于bash -x的效果)
    cinder swift的区别
    C#中Main函数为什么要static
    C#编程.函数.委托
    C#编程.函数.Main()函数
    C#编程.函数.参数
    typedef int a[10];怎么解释?
    C#的DateTime得到特定日期
  • 原文地址:https://www.cnblogs.com/yanmk/p/9448885.html
Copyright © 2011-2022 走看看