代码随想录算法训练营day14 | 二叉树理论基础,递归遍历,迭代遍历,统一迭代法

二叉树理论基础:

  1. https://www.bilibili.com/video/BV1Hy4y1t7ij
  2. 代码随想录
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

递归遍历

  • 递归三部曲:

    1. 确定递归函数的参数和返回值

    2. 确定终止条件

    3. 确定单层递归的逻辑

  • 144. 二叉树的前序遍历-preorder traversal 

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def preorderTraversal(self, root): #step1
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        #step2
        if root == None:
            return []
        #step3
        return [root.val]+self.preorderTraversal(root.left)+self.preorderTraversal(root.right)
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        
        def traversal(root): #step1: parameter and return type
            #step2: base condition
            if root is None:
                return res
            
            #step3: logic
            res.append(root.val)
            traversal(root.left)
            traversal(root.right)
            
        traversal(root)
        return res
        
  • 145. 二叉树的后序遍历-postorder traversal

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def postorderTraversal(self, root): #step1
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        #step2
        if root == None:
            return []
        #step3
        return self.postorderTraversal(root.left)+self.postorderTraversal(root.right)+[root.val]
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        
        def traversal(root): #step1: parameter and return type
            #step2: base condition
            if root is None:
                return res
            
            #step3: logic
            traversal(root.left)
            traversal(root.right)
            res.append(root.val)
            
        traversal(root)
        return res
  • 94. 二叉树的中序遍历-inorder traversal

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def inorderTraversal(self, root): #step 1
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        #step2
        if root is None:
            return []
        #step3
        return self.inorderTraversal(root.left)+[root.val]+self.inorderTraversal(root.right)
        
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        
        def traversal(root): #step1: parameter and return type
            #step2: base condition
            if root is None:
                return res
            
            #step3: logic
            traversal(root.left)
            res.append(root.val)
            traversal(root.right)
            
        traversal(root)
        return res
        

迭代遍历

  • trick:深度优先遍历借助栈,层序优先遍历借助队列

  • 144. 二叉树的前序遍历-preorder traversal 

    • 前序遍历是中左右,那么在用栈的时候就需要先放入右子树,然后左子树,这样在pop的时候就是左子树先出。
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        ans = []
        stack = [root] #初始化需要将root传入,也就是把二叉树传进来;注意:stack存的是指针
        
        if root is None:
            return ans
        
        while stack:
            node = stack.pop()
            #中
            ans.append(node.val)
            #右
            if node.right:
                stack.append(node.right)
            #左
            if node.left:
                stack.append(node.left)
                
        return ans
  • 145. 二叉树的后序遍历-postorder traversal

    • 后序遍历是左右中,那么为了尽量让代码统一,则思考如何在前序遍历的基础来改动。因此,(前序)中左右 --> 中右左 --> 反转 --> 左右中(后序)
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        ans = []
        stack = [root] #初始化需要将root传入,也就是把二叉树传进来;注意:stack存的是指针
        
        if root is None:
            return ans
        
        while stack:
            node = stack.pop()
            #中
            ans.append(node.val)
            #左
            if node.left:
                stack.append(node.left)
            #右
            if node.right:
                stack.append(node.right)
                
        return ans[::-1]
  •  94. 二叉树的中序遍历-inorder traversal

    • 注意访问(遍历)以及处理(将元素放进结果集)的顺序
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        ans = []
        stack =[] #注意:不可初始化的时候传入root,因为顺序是左中右
        curr = root 
        
        if root is None:
            return ans
        
        while curr or stack:
            # 先迭代访问最底层的左子树结点
            if curr:
                stack.append(curr) # 栈就是记录指针访问过的元素,但是需要注意的是栈依旧是存的指针!
                curr =curr.left
            # 到达最左结点后处理栈顶结点  
            else:
                curr = stack.pop()
                ans.append(curr.val) 
                curr = curr.right
                
        return ans

二叉树的统一迭代法

  • 暂时略过

总结:

  • 注意二叉树的定义,基础还要反复看

  • 递归法的三步:从简单的练习

  • 迭代法:前中后序如何利用栈来遍历。

  • 深度优先用栈,广度优先用队列

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