迭代 递归 反转链表 反转二叉树

https://leetcode-cn.com/problems/reverse-linked-list/

反转链表  迭代 递归

https://leetcode-cn.com/problems/invert-binary-tree/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func swap(root *TreeNode) {
    if root != nil {
        t := root.Left
        root.Left = root.Right
        root.Right = t
    }
}
func invertTree(root *TreeNode) *TreeNode {
    if root != nil {
        swap(root)
        invertTree(root.Left)
        invertTree(root.Right)
    }
    return root
}

  https://leetcode-cn.com/problems/invert-binary-tree/solution/fan-zhuan-er-cha-shu-by-leetcode-solution/

1
2
3
4
5
6
7
8
9
10
func invertTree(root *TreeNode) *TreeNode {
    if root == nil {
        return nil
    }
    left := invertTree(root.Left)
    right := invertTree(root.Right)
    root.Left = right
    root.Right = left
    return root
}

  

https://leetcode.cn/problems/swap-nodes-in-pairs/solution/liang-liang-jiao-huan-lian-biao-zhong-de-jie-di-91/

 

posted @   papering  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2021-04-19 Jenkins中无法启动子进程的解决办法
2021-04-19 Gin Any
2021-04-19 Executing a Bash Script from Golang
2021-04-19 C++11 之 override - 飞鸢逐浪 - 博客园 https://www.cnblogs.com/xinxue/p/5471708.html marked ‘override’, but does not override
2021-04-19 以微博核心业务为例,解读如何仅用1台服务器支持百万DAU
2020-04-19 浏览器 页面 线程 进程
2019-04-19 携程框架团队对于应用监控系统的探索与思考
点击右上角即可分享
微信分享提示