• 代码随想录算法训练营第十四天|二叉树理论基础|二叉树的递归遍历|二叉树的迭代遍历以及统一迭代


    今天是代码随想录算法训练营第十四天

    今天学习了

    1. 二叉树理论基础
    2. 二叉树的递归遍历
    3. 二叉树的迭代遍历以及统一遍历

    对于迭代遍历部分还需要再挑战挑战的。

    二叉树的递归遍历代码如下:

    # 前序遍历-递归-LC144_二叉树的前序遍历
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, val=0, left=None, right=None):
    #         self.val = val
    #         self.left = left
    #         self.right = right
    
    class Solution:
        def preorderTraversal(self, root: TreeNode) -> List[int]:
            if not root:
                return []
    
            left = self.preorderTraversal(root.left)
            right = self.preorderTraversal(root.right)
    
            return  [root.val] + left +  right
    
    
    
    # 中序遍历-递归-LC94_二叉树的中序遍历
    class Solution:
        def inorderTraversal(self, root: TreeNode) -> List[int]:
            if root is None:
                return []
    
            left = self.inorderTraversal(root.left)
            right = self.inorderTraversal(root.right)
    
            return left + [root.val] + right
    
    
    
    
    # 后序遍历-递归-LC145_二叉树的后序遍历
    class Solution:
        def postorderTraversal(self, root: TreeNode) -> List[int]:
            if not root:
                return []
    
            left = self.postorderTraversal(root.left)
            right = self.postorderTraversal(root.right)
    
            return left + right + [root.val]
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    二叉树的迭代遍历代码如下:

    这里前序和后序的代码逻辑相似,但是中序的代码逻辑是不同的

    # 前序遍历-迭代-LC144_二叉树的前序遍历
    class Solution:
        def preorderTraversal(self, root: TreeNode) -> List[int]:
            # 根结点为空则返回空列表
            if not root:
                return []
            stack = [root]
            result = []
            while stack:
                node = stack.pop()
                # 中结点先处理
                result.append(node.val)
                # 右孩子先入栈
                if node.right:
                    stack.append(node.right)
                # 左孩子后入栈
                if node.left:
                    stack.append(node.left)
            return result
    
    
    # 中序遍历-迭代-LC94_二叉树的中序遍历
    class Solution:
        def inorderTraversal(self, root: TreeNode) -> List[int]:
            if not root:
                return []
            stack = []  # 不能提前将root结点加入stack中
            result = []
            cur = root
            while cur or stack:
                # 先迭代访问最底层的左子树结点
                if cur:     
                    stack.append(cur)
                    cur = cur.left		
                # 到达最左结点后处理栈顶结点    
                else:		
                    cur = stack.pop()
                    result.append(cur.val)
                    # 取栈顶元素右结点
                    cur = cur.right	
            return result
    
    
    
    
    # 后序遍历-迭代-LC145_二叉树的后序遍历
    class Solution:
       def postorderTraversal(self, root: TreeNode) -> List[int]:
           if not root:
               return []
           stack = [root]
           result = []
           while stack:
               node = stack.pop()
               # 中结点先处理
               result.append(node.val)
               # 左孩子先入栈
               if node.left:
                   stack.append(node.left)
               # 右孩子后入栈
               if node.right:
                   stack.append(node.right)
           # 将最终的数组翻转
           return result[::-1]
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    统一迭代(这种方式的话,三种迭代遍历的逻辑是相似的)

    # 迭代法前序遍历:
    class Solution:
        def preorderTraversal(self, root: TreeNode) -> List[int]:
            result = []
            st= []
            if root:
                st.append(root)
            while st:
                node = st.pop()
                if node != None:
                    if node.right: #右
                        st.append(node.right)
                    if node.left: #左
                        st.append(node.left)
                    st.append(node) #中
                    st.append(None)
                else:
                    node = st.pop()
                    result.append(node.val)
            return result
    
    
    
    # 迭代法中序遍历:
    class Solution:
        def inorderTraversal(self, root: TreeNode) -> List[int]:
            result = []
            st = []
            if root:
                st.append(root)
            while st:
                node = st.pop()
                if node != None:
                    if node.right: #添加右节点(空节点不入栈)
                        st.append(node.right)
                    
                    st.append(node) #添加中节点
                    st.append(None) #中节点访问过,但是还没有处理,加入空节点做为标记。
                    
                    if node.left: #添加左节点(空节点不入栈)
                        st.append(node.left)
                else: #只有遇到空节点的时候,才将下一个节点放进结果集
                    node = st.pop() #重新取出栈中元素
                    result.append(node.val) #加入到结果集
            return result
    
    
    
    # 迭代法后序遍历:
    class Solution:
        def postorderTraversal(self, root: TreeNode) -> List[int]:
            result = []
            st = []
            if root:
                st.append(root)
            while st:
                node = st.pop()
                if node != None:
                    st.append(node) #中
                    st.append(None)
                    
                    if node.right: #右
                        st.append(node.right)
                    if node.left: #左
                        st.append(node.left)
                else:
                    node = st.pop()
                    result.append(node.val)
            return result
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
  • 相关阅读:
    邮箱格式检测易语言代码
    如何去开发一个springboot starter
    纯CSS如何禁止用户复制网页的内容?
    2022年SQL经典面试题总结(带解析)
    CV预测:快速使用DenseNet神经网络
    BPM是什么意思?BPM的优势及好处有哪些?
    2023年09月青少年软件编程(C语言)等级考试试卷(四级)
    时间轴_显示器
    Split Into Two Sets Codeforces 1702E
    猿创征文 |【SpringBoot2】基础配置详解
  • 原文地址:https://blog.csdn.net/qq_42839893/article/details/133039307