• 二叉树的前序、中序、后序遍历的递归和非递归实现


    一、二叉树的前序遍历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]
  • 相关阅读:
    AngularJs学习笔记Understanding Angular Templates
    AngularJs学习笔记Creating Services
    AngularJs学习笔记E2E Testing
    AngularJs学习笔记Using $location
    AngularJs学习笔记Injecting Services Into Controllers
    AngularJs学习笔记unittesting
    AngularJs学习笔记Guide教程系列文章索引
    C语言 自幂数
    C# 虚拟键盘核心方法 Tech
    [转]SQLite SQL语句结构详解 Tech
  • 原文地址:https://www.cnblogs.com/yanmk/p/9448885.html
Copyright © 2020-2023  润新知