• 二叉树 | 翻转二叉树 | leecode刷题笔记


    跟随carl代码随想录刷题
    语言:python


    226. 简单翻转二叉树

    题目:给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
    在这里插入图片描述
    输入:root = [4,2,7,1,3,6,9]
    输出:[4,7,2,9,6,3,1]

    题目分析

    思路:交换每个节点的左右节点即可root.left, root.right = root.right, root.left
    方法选择:

    • 前序遍历💚✔
    • 中序遍历【❌不能用,因为会把所有节点交换两次】
    • 后序遍历💚✔
    • 层序遍历💚✔

    完整代码如下

    方法1.1:前序遍历——递归法

    # 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
            if root == None:
                return None
            
            # 交换节点
            root.left, root.right = root.right, root.left  # 中
            self.invertTree(root.left)  # 左
            self.invertTree(root.right)  # 右
    
            return root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    方法1.2:后序遍历——递归法

    # 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
            if root == None:
                return None
            
            self.invertTree(root.left)  # 左
            self.invertTree(root.right)  # 右
    
            # 交换节点
            root.left, root.right = root.right, root.left  # 中
    
            return root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    方法2.1:前序遍历——迭代法

    # 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
            
            if not root:
                return None
    
            st = []
            st.append(root)
            while st:
                node = st.pop()
                node.left, node.right = node.right, node.left  # 中
                if node.right:
                    st.append(node.right)  # 左
                if node.left:
                    st.append(node.left)  # 右
            return root 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    方法2.2:后序遍历——迭代法

    # 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
            
            if not root:
                return None
    
            st = []
            st.append(root)
            while st:
                node = st.pop()
                
                if node.right:
                    st.append(node.right)  # 左
                if node.left:
                    st.append(node.left)  # 右
                node.left, node.right = node.right, node.left  # 中
            return root 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    方法3:层序遍历

    # 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
            
            if not root:
                return None
            
            from collections import deque
            que = deque([root])
    
            while que:
                size = len(que)
                cur = que.popleft()
                cur.left, cur.right = cur.right, cur.left
                if cur.left:
                    que.append(cur.left)
                if cur.right:
                    que.append(cur.right)
            return root
    
    
    • 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
  • 相关阅读:
    【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
    众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
    win10pycharm和anaconda安装和环境配置教程
    ThreadPoolExecutor 基础入门
    python中scipy中uniform分布怎么用
    2023IG新功能大整理,更多玩法助力营销推广
    纯手写http服务器
    第2-3-4章 上传附件的接口开发-文件存储服务系统-nginx/fastDFS/minio/阿里云oss/七牛云oss
    操作系统:文件IO
    vue3(二)
  • 原文地址:https://blog.csdn.net/qq_44250700/article/details/126269475