Golang | Leetcode Golang题解之第114题二叉树展开为链表

题目:

题解:

Go 复制代码
func flatten(root *TreeNode)  {
    curr := root
    for curr != nil {
        if curr.Left != nil {
            next := curr.Left
            predecessor := next
            for predecessor.Right != nil {
                predecessor = predecessor.Right
            }
            predecessor.Right = curr.Right
            curr.Left, curr.Right = nil, next
        }
        curr = curr.Right
    }
}
相关推荐
We་ct3 分钟前
LeetCode 138. 随机链表的复制:两种最优解法详解
前端·算法·leetcode·链表·typescript
Mr -老鬼20 分钟前
基于 Go 的脚本平台 APP 云控系统
开发语言·后端·golang
Hag_2025 分钟前
LeetCode Hot100 42.接雨水
算法·leetcode·职场和发展
老鼠只爱大米35 分钟前
LeetCode经典算法面试题 #153:寻找旋转排序数组中的最小值(暴力搜索、二分查找等五种实现方案详细解析)
算法·leetcode·二分查找·旋转数组·最小值搜索
大鹏说大话1 小时前
深入理解 Go 中的 make(chan chan error):高阶通道的典型用法与实战场景
开发语言·后端·golang
TracyCoder1231 小时前
LeetCode Hot100(56/100)——131. 分割回文串
算法·leetcode
YGGP2 小时前
【Golang】LeetCode 42. 接雨水
算法·leetcode·职场和发展
@––––––2 小时前
力扣hot100—系列7-二分查找
数据结构·算法·leetcode
鲨鱼吃橘子3 小时前
C++刷题--递归回溯剪枝(二)
开发语言·数据结构·c++·算法·leetcode·深度优先·剪枝
TracyCoder12317 小时前
LeetCode Hot100(46/100)——74. 搜索二维矩阵
算法·leetcode·矩阵