LeetCode226:反转二叉树

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

在这里插入图片描述
解题思想
使用前序遍历和后序遍历比较方便

代码

class Solution {
public:
   
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) return root;
        
        swap(root->left, root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

相关推荐

  1. 力扣 226.

    2024-04-01 22:42:06       57 阅读
  2. LeetCode 2415. 的奇数层

    2024-04-01 22:42:06       62 阅读
  3. LeetCode——2415. 的奇数层

    2024-04-01 22:42:06       64 阅读
  4. LeetCode226.翻转

    2024-04-01 22:42:06       39 阅读
  5. Leetcode226.翻转

    2024-04-01 22:42:06       21 阅读

最近更新

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

    2024-04-01 22:42:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 22:42:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 22:42:06       87 阅读
  4. Python语言-面向对象

    2024-04-01 22:42:06       96 阅读

热门阅读

  1. 氯丁橡胶衬板是什么

    2024-04-01 22:42:06       34 阅读
  2. 二阶系统与环境相互作用

    2024-04-01 22:42:06       35 阅读
  3. 自定义函数的使用

    2024-04-01 22:42:06       41 阅读
  4. redis -String类型用法

    2024-04-01 22:42:06       36 阅读
  5. UDP协议

    UDP协议

    2024-04-01 22:42:06      33 阅读
  6. LeetCode 每日一题 2024/3/25-2024/3/31

    2024-04-01 22:42:06       34 阅读
  7. Ubuntu+Caddy:免费服务器上部署WordPress!

    2024-04-01 22:42:06       33 阅读
  8. 数据库【QSqlTableModel】

    2024-04-01 22:42:06       29 阅读
  9. SpringMVC源码分析(九)--返回值解析器

    2024-04-01 22:42:06       38 阅读
  10. nodejs的express编写http服务器配置跨域

    2024-04-01 22:42:06       40 阅读