Python实现二叉树的前,中,后,层序遍历(递归与迭代)

Python实现二叉树的前,中,后,层序遍历(递归与迭代)

  • 1.前序遍历
    • 递归实现
    • 迭代实现
  • 2.中序遍历
    • 递归实现
    • 迭代实现
  • 3.后序遍历
    • 递归实现
    • 迭代实现
  • 4.层序遍历

1.前序遍历

递归实现

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

迭代实现

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

2.中序遍历

递归实现

class Solution:
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res=[]
        self.dfs(root,res)
        return res
    def dfs(self,root,r):
        if root:
        	self.dfs(root.left,r)
            r.append(root.val)
            self.dfs(root.right,r)

迭代实现

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

3.后序遍历

递归实现

class Solution:
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res=[]
        self.dfs(root,res)
        return res
    def dfs(self,root,r):
        if root:
            self.dfs(root.left,r)
            self.dfs(root.right,r)
            r.append(root.val)

迭代实现

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

4.层序遍历

class Solution:
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        res=[]
        level=[root]
        while level:
            res.append([node.val for node in level])
            temp=[]
            for node in level:
                temp.extend([node.left,node.right])
            level=[leaf for leaf in temp if leaf]
        return res

你可能感兴趣的:(Python实现二叉树的前,中,后,层序遍历(递归与迭代))