代码随想录训练营Day14|二叉树理论基础●递归遍历 ● 迭代遍历● 统一迭代

目录

学习目标

学习内容

 递归遍历

迭代遍历 


学习目标

  • 理论基础
  • 递归遍历
  • 迭代遍历
  • 统一迭代

学习内容

problems/二叉树理论基础.md · programmercarl/leetcode-master(代码随想录出品) - Gitee.comicon-default.png?t=N2N8https://gitee.com/programmercarl/leetcode-master/blob/master/problems/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.md 

递归遍历

problems/二叉树的递归遍历.md · programmercarl/leetcode-master(代码随想录出品) - Gitee.comicon-default.png?t=N2N8https://gitee.com/programmercarl/leetcode-master/blob/master/problems/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.md

迭代遍历 

problems/二叉树的迭代遍历.md · programmercarl/leetcode-master(代码随想录出品) - Gitee.comicon-default.png?t=N2N8https://gitee.com/programmercarl/leetcode-master/blob/master/problems/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%BF%AD%E4%BB%A3%E9%81%8D%E5%8E%86.md

144. 二叉树的前序遍历 - 力扣(LeetCode)icon-default.png?t=N2N8https://leetcode.cn/problems/binary-tree-preorder-traversal/根左右 

# 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: Optional[TreeNode]) -> List[int]:
        if not root:return []
        head = [root.val]
        left = self.preorderTraversal(root.left)
        right = self.preorderTraversal(root.right)
        return head+left+right
# 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: Optional[TreeNode]) -> List[int]:
        if not root:return []
        stack = [root]
        res = []
        while stack:
            e = stack.pop()
            res.append(e.val)
            if e.right:
                stack.append(e.right)
            if e.left:
                stack.append(e.left)
        return res


94. 二叉树的中序遍历 - 力扣(LeetCode)icon-default.png?t=N2N8https://leetcode.cn/problems/binary-tree-inorder-traversal/

左根右

# 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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:return []
        left = self.inorderTraversal(root.left)
        head = [root.val]
        right = self.inorderTraversal(root.right)
        return left+head+right
# 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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:return []
        stack = [root]
        res = []
        while stack:
            e = stack.pop()
            if e:
                if e.right:
                    stack.append(e.right)
                stack.append(e)
                stack.append(None)
                if e.left:
                    stack.append(e.left)  
            else:
                res.append(stack.pop().val)      
        return res

 

 145. 二叉树的后序遍历 - 力扣(LeetCode)icon-default.png?t=N2N8https://leetcode.cn/problems/binary-tree-postorder-traversal/

左右根

# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:return []
        left = self.postorderTraversal(root.left)
        right = self.postorderTraversal(root.right)
        head = [root.val]
        return left+right+head
# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:return []
        stack = [root]
        res = []
        while stack:
            e = stack.pop()
            if e:
                stack.append(e)
                stack.append(None) 
                if e.right:
                    stack.append(e.right)
                if e.left:
                    stack.append(e.left) 
            else:
                res.append(stack.pop().val)      
        return res

你可能感兴趣的:(算法,数据结构,python,深度优先)