数据结构与算法13-前、后、中序遍历代码框架

前、后、中序遍历的递归和迭代代码框架

前:根左右
后:左右根
中:左根右

递归版本对于三种方法具有一致性:

# 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]
        """
        res = []
        def helper(proot):
            if proot is None:
                return
            res.append(proot.val)  # 移动这个append-"根"即可
            left = helper(proot.left)
            right = helper(proot.right)
        helper(root)
        return res

迭代方法各异,但前、后序相似,中序单独考虑

1. 前序

# 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]
        """
        res = []  # 里面放val
        if root is None:
            return res
             
        stack = [root] # 先进后出,所以右先进,这样右后出。

        while stack:
            node = stack.pop()
            res.append(node.val)
            if node.right is not None:
                stack.append(node.right)
            if node.left is not None:
                stack.append(node.left)

        return res

2. 后序("左右根"加反转)

# 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]
        """
        res = []  # 里面放val
        if root is None:
            return res
             
        stack = [root]

        while stack:
            node = stack.pop()       
            if node.left is not None:  # left、right顺序和前序相反
                stack.append(node.left)
            if node.right is not None:
                stack.append(node.right)
            res.append(node.val)  # 左右根

        return res[::-1]  # 反转

3. 中序

# 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]
        """
        res = []
        if root is None:
            return res
        stack = []
        cur = root
        while stack or cur:
            while cur:  # 把当前节点及其左节点、左节点的左节点...放入stack
                stack.append(cur)
                cur = cur.left
            cur = stack.pop()
            res.append(cur.val)  # 取出最后一个左节点,并将其值放入res
            cur = cur.right  # 取一次右节点
        return res

所有方法:
时间复杂度:访问每个节点恰好一次,时间复杂度为 O ( N ) O(N) ,其中 N N 是节点的个数,也就是树的大小。
空间复杂度:取决于树的结构,最坏情况存储整棵树,因此空间复杂度是 O ( N ) O(N)

发布了71 篇原创文章 · 获赞 20 · 访问量 4842

猜你喜欢

转载自blog.csdn.net/qq_22795223/article/details/105142684