当前位置:网站首页>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 :
边栏推荐
- Speculation London gold short-term stable money making skills? Where is it safe to fry London gold?
- Popular science of data annotation: ten common image annotation methods
- Nacos source code - configure automatic update
- 最新热点:使用铜死亡相关基因进行肿瘤预后分型!
- The opportunity to teach cloud development a lesson has finally come!
- JVM GC garbage collection detailed introduction quick check of learning notes
- 如何优雅的写 Controller 层代码?
- How to purchase new bonds is it safe to open an account
- Coinbase will launch the first encrypted derivative product for retail traders
- Tencent cloud and the ICT Institute launched the preparation of the "cloud native open source white paper" to deeply interpret cloud native
猜你喜欢

Tools and methods - use code formatting tools in source insight

Qt: judge whether the string is in numeric format

How is the e-commerce red envelope realized? For interview (typical high concurrency)

【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献

Opencv learning notes - loading and saving images

软件测试 对前一日函数的基本路径测试

《opencv学习笔记》-- 图像的载入和保存
Cloud native database: the outlet of the database, you can also take off

New progress in the construction of meituan's Flink based real-time data warehouse platform

【数字IC/FPGA】Booth乘法器
随机推荐
不用做实验的6分+基因家族纯生信思路~
电商红包雨是如何实现的?拿去面试用(典型高并发)
11+! Methylation modification patterns based on m6A regulatory factors in colon cancer are characterized by different tumor microenvironment immune spectra
GLOG from getting started to getting started
QT -- the qtabwidget supports dragging tabbar items
2021-06-03: Boolean operation. Given a Boolean expression and an expected cloth
《opencv学习笔记》-- 分离颜色通道、多通道混合
Flink snapshot analysis: operators for locating large states and data skew
如何优雅的写 Controller 层代码?
Example of PHP observer mode [useful in the laravel framework]
Nacos source code - configure automatic update
怎么可以打新债 开户是安全的吗
I'm in Shenzhen. Where can I open an account? Is it safe to open an account online now?
Is it safe to open an account under the conditions of new bonds
5分+的单基因泛癌纯生信思路!
11+! 结肠癌中基于 m6A 调节因子的甲基化修饰模式以不同的肿瘤微环境免疫谱为特征
How to apply for new bonds is it safe to open an account
《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合
[digital ic/fpga] booth multiplier
怎样申请打新债 开户是安全的吗