LeetCode 热题 HOT 100 (005/100)【宇宙最简单版】

【二叉树】No. 0226 翻转二叉树【简单】👉力扣对应题目指路

希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
欢迎关注、订阅专栏 【力扣详解】谢谢你的支持!

题目描述:给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

  • 示例: [4,2,7,1,3,6,9] -> [4,7,2,9,6,3,1]

🔥 思路:由底向上(后序遍历),逐个翻转每一个子树

  • 后续描述以由底向上(后序遍历)展开
  • 由上向底(前序遍历) 也可,在代码中已注释标注

⭐题目准备之后续遍历:一定要先掌握后续遍历 ❗❗❗ 放在最后面啦,供不熟悉的友友参考~

参考如上思路,给出详细步骤如下:

  • 步骤一⭐确定递归函数 traversal 参数及返回值
    • 参数:仅需要获取当前需要翻转的根节点 current_node
    • 返回值:不需要返回值
  • 步骤二⭐确定递归终止条件:走到了 None 节点
    • 说明当前下面没有待反转子树
  • 步骤三⭐确定单层递归逻辑-后序处理:交换当前节点左右节点
# 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]:
        def traversal(current_node):  # ----------------- step 1
            if current_node == None:  # ----------------- step 2
                return
            
            # -- 前序处理的话,移动 step 3 到位置
            
            traversal(current_node.left)
            traversal(current_node.right)
			
			# ------------------------------------------ step 3
            temp = current_node.left
            current_node.left = current_node.right
            current_node.right = temp
        
        traversal(root)
        return root

⭐⭐⭐ 题目准备之后续遍历:一定要先掌握后续遍历 ❗❗❗

# 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 lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        self.result = []

        def traversal(current):
            if current == None:
                return
            print('-------------------------Hi, node: ', current.val)
            traversal(current.left)
            traversal(current.right)
            print('----- current_val: ', current.val)  // 观察此处的处理顺序,是后序
            self.result.append(current.val)
        
        traversal(root)  ## self.result: [6, 7, 4, 2, 5, 0, 8, 1, 3]
  • 对应的输出结果如下:
    ('-------------------------Hi, node: ', 3)
    ('-------------------------Hi, node: ', 5)
    ('-------------------------Hi, node: ', 6)
    ('----- current_val: ', 6)
    ('-------------------------Hi, node: ', 2)
    ('-------------------------Hi, node: ', 7)
    ('----- current_val: ', 7)
    ('-------------------------Hi, node: ', 4)
    ('----- current_val: ', 4)
    ('----- current_val: ', 2)
    ('----- current_val: ', 5)
    ('-------------------------Hi, node: ', 1)
    ('-------------------------Hi, node: ', 0)
    ('----- current_val: ', 0)
    ('-------------------------Hi, node: ', 8)
    ('----- current_val: ', 8)
    ('----- current_val: ', 1)
    ('----- current_val: ', 3)
    
    

希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
欢迎关注、订阅专栏 【力扣详解】谢谢你的支持!
🔥 LeetCode 热题 HOT 100

相关推荐

  1. leetcodeHOT 155. 小栈

    2024-07-21 22:18:04       33 阅读
  2. leetcodeHOT 32. 长有效括号

    2024-07-21 22:18:04       35 阅读
  3. LeetCodeHot100 - 长有效括号

    2024-07-21 22:18:04       30 阅读
  4. leetcodeHOT146. LRU 缓存

    2024-07-21 22:18:04       34 阅读
  5. LeetCodeHot100 - 括号生成

    2024-07-21 22:18:04       36 阅读
  6. LeetCodeHot100-无重复字符的长子串

    2024-07-21 22:18:04       44 阅读
  7. LeetCodeHot100 - 长回文子串

    2024-07-21 22:18:04       38 阅读
  8. LeetCodeHot100 - 盛水多的容器

    2024-07-21 22:18:04       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 22:18:04       101 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 22:18:04       109 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 22:18:04       87 阅读
  4. Python语言-面向对象

    2024-07-21 22:18:04       96 阅读

热门阅读

  1. Python利用psutil库进行监控进程和资源

    2024-07-21 22:18:04       25 阅读
  2. SpringBoot RestHighLevelClient 按版本更新

    2024-07-21 22:18:04       28 阅读
  3. 跨域问题几种解决方法

    2024-07-21 22:18:04       28 阅读
  4. Python面试整理-文件处理

    2024-07-21 22:18:04       22 阅读
  5. 分式

    2024-07-21 22:18:04       26 阅读
  6. Spring WebFlux 介绍与效果演示示例

    2024-07-21 22:18:04       29 阅读
  7. django 需要修改的文件

    2024-07-21 22:18:04       27 阅读
  8. Random,ThreadLocalRandom,SecureRandom有什么区别

    2024-07-21 22:18:04       25 阅读
  9. Python 爬虫技术 第05节 异常处理

    2024-07-21 22:18:04       27 阅读
  10. 微信小程序开发:DOM 相关 API 使用详解

    2024-07-21 22:18:04       24 阅读
  11. QtQuick-QML语法

    2024-07-21 22:18:04       26 阅读
  12. Codeforces Round 960 (Div. 2)VP

    2024-07-21 22:18:04       25 阅读
  13. WebAssembly在前端开发中的创新与应用

    2024-07-21 22:18:04       24 阅读
  14. easyExcel

    easyExcel

    2024-07-21 22:18:04      24 阅读
  15. 什么是等保测评

    2024-07-21 22:18:04       23 阅读