当前位置:网站首页>2021-06-02: given the head node of a search binary tree, it will be transformed into an ordered two-way linked list with head and tail connected.
2021-06-02: given the head node of a search binary tree, it will be transformed into an ordered two-way linked list with head and tail connected.
2022-06-24 12:20:00 【Fuda scaffold constructor's daily question】
2021-06-02: Given a search binary tree head node , Turn into an orderly two-way linked list connected from beginning to end .
Fuda answer 2021-06-02:
Binary tree recursion . The left subtree is finished , The right subtree is finished , Eventually string yourself .
The code to use golang To write . The code is as follows :
package main import "fmt" func main() { head := &Node{Val: 5} head.Left = &Node{Val: 3} head.Left.Left = &Node{Val: 1} head.Left.Right = &Node{Val: 4} head.Right = &Node{Val: 7} head.Right.Left = &Node{Val: 6} head.Right.Right = &Node{Val: 8} ret := treeToDoublyList(head) for i := 0; i < 20; i++ { fmt.Println(ret) ret = ret.Right } } type Node struct { Val int Left *Node Right *Node } func treeToDoublyList(head *Node) *Node { if head == nil { return nil } allInfo := process(head) allInfo.End.Right = allInfo.Start allInfo.Start.Left = allInfo.End return allInfo.Start } type Info struct { Start *Node End *Node } func process(X *Node) *Info { if X == nil { return &Info{} } lInfo := process(X.Left) rInfo := process(X.Right) if lInfo.End != nil { lInfo.End.Right = X } X.Left = lInfo.End X.Right = rInfo.Start if rInfo.Start != nil { rInfo.Start.Left = X } // The head of the whole linked list lInfo.start != null ? lInfo.start : X // Tail of the whole linked list rInfo.end != null ? rInfo.end : X return &Info{twoSelectOne(lInfo.Start != nil, lInfo.Start, X), twoSelectOne(rInfo.End != nil, rInfo.End, X)} } func twoSelectOne(c bool, a *Node, b *Node) *Node { if c { return a } else { return b } }
The results are as follows :
边栏推荐
- Basic path test of software test on the function of the previous day
- Use go to process millions of requests per minute
- How to develop hospital information system (his) with SMS notification and voice function
- 怎样购买打新债 开户是安全的吗
- Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 1)
- Tools and methods - use code formatting tools in source insight
- 深圳市人民医院程立新课题组提出多组学数据在肝细胞癌的诊断与预后分析的新方法meGPS
- 【云驻共创】解读HarmonyOS 应用与服务生态
- TP-LINK 1208 router tutorial (2)
- 基于AM335X开发板 ARM Cortex-A8——Acontis EtherCAT主站开发案例
猜你喜欢
工具及方法 - 在Source Insight中使用代码格式化工具
Group planning - General Review
软件测试 对前一日函数的基本路径测试
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
计组-总复习
Insurance app aging service evaluation analysis 2022 issue 06
Qt: judge whether the string is in numeric format
Opencv learning notes -- Separation of color channels and multi-channel mixing
保险APP适老化服务评测分析2022第06期
[mysql_16] variables, process control and cursors
随机推荐
Collation of related papers on root cause analysis
分布式系统解决之道:目录、消息队列、事务系统及其他
Is it safe to open an account under the conditions of new bonds
C语言循环语句介绍(foe、while、do...while)
11+ article - machine learning builds Protics framework - deeply reveals the impact of tumor infiltrating immune cells in different molecular subtypes on prognosis
GLOG from getting started to getting started
I'm in Shenzhen. Where can I open an account? Is it safe to open an account online now?
Group planning - General Review
LS-DYNA beginner's experience
可变参数模板实现max(接受多个参数,两种实现方式)
计组-总复习
2021-06-03: Boolean operation. Given a Boolean expression and an expected cloth
FreeRTOS概述与体验
The idea of "6 points + gene family" without experiment~
The opportunity to teach cloud development a lesson has finally come!
Opencv learning notes - Discrete Fourier transform
Opencv learning notes - loading and saving images
Cluster control management
ahk实现闹钟
Using the collaboration database query of Poole in laravel5.6