当前位置:网站首页>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 :
边栏推荐
- TP-LINK 1208 router tutorial (2)
- PF_ Ring ZC | high speed traffic processing dpdk alternative
- Jenkins performance test
- 计组-总复习
- The opportunity to teach cloud development a lesson has finally come!
- Listed JD Logistics: breaking through again
- 10 zeros of D
- 怎么申请打新债 开户是安全的吗
- Opencv learning notes - cv:: mat class
- How to apply for new bonds is it safe to open an account
猜你喜欢

Qt: judge whether the string is in numeric format

我真傻,招了一堆只会“谷歌”的程序员!

《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合

GLOG from getting started to getting started

《梦华录》要大结局了,看超前点映不如先来学学它!

Opencv learning notes - regions of interest (ROI) and image blending

Programmers spend most of their time not writing code, but...

Basic path test of software test on the function of the previous day

《opencv学习笔记》-- 分离颜色通道、多通道混合

QT -- the qtabwidget supports dragging tabbar items
随机推荐
[mysql_16] variables, process control and cursors
10 zeros of D
What are the low threshold financial products in 2022? Not much money
[206] use PHP language to generate the code of go language
How to check the situation that the national standard platform easygbs equipment video cannot be accessed by grabbing packets?
单基因泛癌+简单实验就能发表7分+
Why does the virtual machine Ping the host but not the virtual machine
What should music website SEO do?
FreeRTOS overview and experience
【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习
Using the collaboration database query of Poole in laravel5.6
How is the e-commerce red envelope realized? For interview (typical high concurrency)
Chenglixin research group of Shenzhen People's hospital proposed a new method of multi group data in the diagnosis and prognosis analysis of hepatocellular carcinoma megps
Is it safe to open an account for how many new bonds you can apply for
嵌入式必学!硬件资源接口详解——基于ARM AM335X开发板 (上)
Ten thousand campus developers play AI in a fancy way. It's enough to see this picture!
不用做实验的6分+基因家族纯生信思路~
Realization of alarm clock with AHK
PF_ Ring ZC | high speed traffic processing dpdk alternative
Is GF Securities reliable? Is it safe to open a securities account?