当前位置:网站首页>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 :
边栏推荐
- 万名校园开发者花式玩AI,亮点看这张图就够啦!
- 怎样打新债具体操作 开户是安全的吗
- 软件测试 对前一日函数的基本路径测试
- [mysql_16] variables, process control and cursors
- 基于AM335X开发板 ARM Cortex-A8——Acontis EtherCAT主站开发案例
- Example of PHP observer mode [useful in the laravel framework]
- 数据标注科普:十种常见的图像标注方法
- Google Earth engine (GEE) - how to add a legend in the map panel
- Opencv learning notes - matrix normalization normalize() function
- How to write controller layer code gracefully?
猜你喜欢

PHP SMS notification + voice broadcast automatic double call

电商红包雨是如何实现的?拿去面试用(典型高并发)
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/fa/e52bd8a1a404a759ef6ba88e8da0f0.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

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

如何优雅的写 Controller 层代码?

GLOG from getting started to getting started

Install Kali on the U disk and persist it

GTEST from getting started to getting started

Opencv learning notes - loading and saving images

Group planning - General Review
随机推荐
ArrayList # sublist these four holes, you get caught accidentally
Database migration tool flyway vs liquibase (II)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 1)
Google Earth engine (GEE) - how to add a legend in the map panel
《opencv学习笔记》-- 离散傅里叶变换
Using the collaboration database query of Poole in laravel5.6
The solution of distributed system: directory, message queue, transaction system and others
怎样打新债具体操作 开户是安全的吗
Ten thousand campus developers play AI in a fancy way. It's enough to see this picture!
Opencv learning notes - cv:: mat class
Opencv learning notes -- Separation of color channels and multi-channel mixing
Opencv learning notes - loading and saving images
Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case
Is it safe to apply for new bonds to open an account
Use go to process millions of requests per minute
Tencent cloud and the ICT Institute launched the preparation of the "cloud native open source white paper" to deeply interpret cloud native
保险APP适老化服务评测分析2022第06期
【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
How to write controller layer code gracefully?