将搜索二叉树转换为链表_将给定的二叉树转换为双链表(DLL)

将搜索二叉树转换为链表

Given a Binary tree and we have to convert it to a Doubly Linked List (DLL).

给定二叉树,我们必须将其转换为双链表(DLL)。

Binary tree to DLL conversion

Algorithm:

算法:

To solve the problem we can follow this algorithm:

为了解决这个问题,我们可以遵循以下算法:

  1. We start to convert the tree node to DLL from the rightmost tree node to the leftmost tree node.

    我们开始从最右边的树节点到最左边的树节点将树节点转换为DLL。

  2. Every time we connect the right pointer of a node to the head of the DLL.

    每次我们将节点的右指针连接到DLL的头时。

  3. Connect the left pointer of the DLL to that node.

    将DLL的左指针连接到该节点。

  4. Make that node to the head of the linked list.

    使该节点位于链接列表的开头。

  5. Repeat the process from right to left most node of the tree.

    从树的最右端到最左端重复该过程。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
node* left;
node* right;
};
//Create a new node
struct node* create_node(int x)
{
struct node* temp = new node;
temp->data = x;
temp->left = NULL;
temp->right = NULL;
return temp;
}
//convert a BST to a DLL
void BinarytoDll(node* root, node** head)
{
if (root == NULL)
return;
BinarytoDll(root->right, head);
root->right = *head;
if (*head != NULL) {
(*head)->left = root;
}
*head = root;
BinarytoDll(root->left, head);
}
//Print the list
void print(node* head)
{
struct node* temp = head;
while (temp) {
cout << temp->data << " ";
temp = temp->right;
}
}
//print the tree in inorder traversal
void print_tree(node* root)
{
if (root == NULL) {
return;
}
print_tree(root->left);
cout << root->data << " ";
print_tree(root->right);
}
int main()
{
struct node* root = create_node(5);
root->left = create_node(6);
root->right = create_node(7);
root->left->left = create_node(8);
root->left->right = create_node(1);
root->right->right = create_node(0);
cout << "Print Tree" << endl;
print_tree(root);
struct node* head = NULL;
BinarytoDll(root, &head);
cout << "\nDoubly Linked List" << endl;
print(head);
return 0;
}

Output

输出量

Print Tree
8 6 1 5 7 0
Doubly Linked List
8 6 1 5 7 0

翻译自: https://www.includehelp.com/cpp-programs/convert-a-given-binary-tree-to-doubly-linked-list-dll.aspx

将搜索二叉树转换为链表

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542546.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cuda编程_CUDA刷新器:CUDA编程模型

CUDA刷新器&#xff1a;CUDA编程模型 CUDA Refresher: The CUDA Programming Model CUDA&#xff0c;CUDA刷新器&#xff0c;并行编程 这是CUDA更新系列的第四篇文章&#xff0c;它的目标是刷新CUDA中的关键概念、工具和初级或中级开发人员的优化。 CUDA编程模型提供了GPU体系结…

php curl_error源码,PHP curl_error函数

PHP curl_error函数(PHP 4 > 4.0.3, PHP 5)curl_error — 返回一个保护当前会话最近一次错误的字符串说明string curl_error ( resource $ch )返回一条最近一次cURL操作明确的文本的错误信息。参数ch由 curl_init() 返回的 cURL 句柄。返回值返回错误信息或 (空字符串) 如果…

SQL中Where与Having的区别

“Where” 是一个约束声明&#xff0c;使用Where来约束来之数据库的数据&#xff0c;Where是在结果返回之前起作用的&#xff0c;且Where中不能使用聚合函数。“Having”是一个过滤声明&#xff0c;是在查询返回结果集以后对查询结果进行的过滤操作&#xff0c;在Having中可以使…

java 逻辑表达式 布尔_使用基本逻辑门实现布尔表达式

java 逻辑表达式 布尔将布尔表达式转换为逻辑电路 (Converting Boolean Expression to Logic Circuit) The simplest way to convert a Boolean expression into a logical circuit is to follow the reverse approach in which we start from the output of the Boolean expre…

python自然语言处理书籍_精通Python自然语言处理pdf

自然语言处理&#xff08;NLP&#xff09;是有关计算语言学与人工智能的研究领域之一。NLP主要关注人机交互&#xff0c;它提供了计算机和人类之间的无缝交互&#xff0c;使得计算机在机器学习的帮助下理解人类语言。 本书详细介绍如何使用Python执行各种自然语言处理&#xff…

通达oa 2013 php解密,通达OA漏洞学习 - 安全先师的个人空间 - OSCHINA - 中文开源技术交流社区...

说明通达OA漏洞在去年上半年已爆出&#xff0c;这不趁着周末没事做&#xff0c;将源码下载下来进行复现学习。文件包含测试文件包含检测&#xff0c;payload1:ip/ispirit/interface/gateway.php?json{"url":"/general/../../mysql5/my.ini"}利用文件包含访…

温赵轮 访谈

“温赵轮”三大软狗&#xff0c;你听说过吗&#xff1f;今天的1024访谈录给大家介绍的就是程序员中当之无愧的偶像组合——温赵轮。 Winter寒冬。阿里P8&#xff0c;正在向P9的道路上奔跑。传说中的他有钱、出身好&#xff0c;可不是搞互联网的屌丝程序员。 老赵&#xff0c;…

linux开源文档管理系统_Linux中的系统管理员问题 免费和开源软件

linux开源文档管理系统根帐号 (Root Account) The "root" account is the most unrestrictive account on a Linux Operating system. This account enables you to complete all features of System admin, including accounts, changing client passwords, looking…

matlab上机实验1,上机实验1:熟悉matlab基本操作

其中 x 在 [-2, 2] 间共等切分为 21 点&#xff0c;y 在 [-1, 1] 间共等切分为 21 点&#xff0c;所以此曲面共有 21*21441 个点。a. 请用预设的颜色对应表(Colormap)来画出此曲面。 b. 请以曲面的斜率来设定曲面的颜色。 c. 请以曲面的曲率来设定曲面的颜色。2. 请用 meshc 指…

公众号 -「前端攻略 开光篇」

作为一枚程序员&#xff0c;每件重要项目的开始都忍不住使用"Hello World"。 这个公众号是不是来晚了&#xff1f;如果你有这个疑问&#xff0c;那么我想说&#xff1a;对于写作和思考&#xff0c;任何时候都不晚。我用四个简单的自问自答&#xff0c;来讲讲这个前端…

python 桌面应用 启动缓慢_如何加快Python 应用的启动时间

我听说pipenv9.0.2已经发布&#xff0c;启动时间有了很大的改进。 我很快就试了一下&#xff0c;但我觉得并不快。所以我用Python3.7的新特性来研究它。 在本文中&#xff0c;我将介绍该特性以及如何使用它。 启动时间≒导入时间 例如&#xff0c;pipenv -h 的执行时间比显示帮…

python单词首字母大写_在Python中将每个单词的首字母大写

python单词首字母大写Here, we are implementing a python program to capitalizes the first letter of each word in a string. 在这里&#xff0c;我们正在实现一个python程序来大写字符串中每个单词的首字母。 Example: 例&#xff1a; Input: "HELLO WORLD!"O…

matlab中求模最大,matlab求取模极大值时出错

本帖最后由 Nate_ 于 2016-4-17 15:57 编辑points1024 时&#xff0c;有波形输出&#xff0c;但信号有5438个点。改为5438就不行。主程序&#xff1a;%小波模极大值重构是采用的交替投影法close all;points5438; level4; sr360; num_inter6; wfdb4;%所处理数据的…

stl向量_如何检查C ++ STL中向量中是否存在元素?

stl向量Given a vector and an element to be searched in the vector. 给定一个向量和要在向量中搜索的元素。 To check whether an elements exists in a vector or not – we use find() function. find() function takes 3 arguments. 要检查向量中是否存在元素 –我们使用…

java socket如何请求485协议_javaSE第十五部分 网络编程(1)Socket和ServerSocket

网络编程基础知识C/S结构&#xff1a;全称为Client/Server结构&#xff0c;是指客户端和服务器结构。常见程序有&#xff31;&#xff31;、迅雷等软件。B/S结构&#xff1a;全称为Browser/Server结构&#xff0c;是指浏览器和服务器结构。常见浏览器有谷歌、火狐等。两种架构各…

【分享】linux下u盘使用

2019独角兽企业重金招聘Python工程师标准>>> linux下u盘使用 方案一&#xff1a; Linux不像Windows一样&#xff0c;接上新硬件后可以自动识别&#xff0c;在Linux下无法自动识别新硬件的&#xff0c;需要手动去识别。USB移动存储设备通常被识别为sda1&#xff0c;…

kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

kotlin中判断字符串Given a string and a character, we have to remove all occurrences of the character in given string. 给定一个字符串和一个字符&#xff0c;我们必须删除给定字符串中所有出现的字符。 Example: 例&#xff1a; Input:string "includeHelp Del…

Java9中使用jpa,jpa – eclipselink在Java 9上使用final字段进行静态编织

我有一些JPA注释字段,如下所示&#xff1a;Column(name "SOME_FIELD", updatable false, nullable false)private final String someField;当实体插入数据库时​​,这些字段存储在数据库中.它们无法进一步更新.对于Java编程语言,可以将这些字段视为final.使用Ecli…

python语言程序设计及医学应用_Python语言程序设计(高等学校计算机专业规划教材)...

第1章Python语言概述/1 1.1Python语言的发展1 1.1.1Python的起源1 1.1.2Python的发展2 1.2Python语言的特点2 1.2.1Python的特性2 1.2.2Python的缺点4 1.2.3Python与其他语言的比较5 1.3简单的Python程序介绍5 1.4Python的程序开发工具8 1.4.1Python的版本选择8 1.4.2Python的安…

swift 3.0 中使用 xib

文章写于2016年9月底&#xff0c;Xcode 8&#xff0c;swift 3.0真是蛋疼&#xff0c;折腾了很长时间&#xff0c;试了网上很多教程&#xff0c;结果又莫名的可以了&#xff01; 1.方法和OC中一样 将一个xib文件和一个ViewController类进行关联的几步操作&#xff1a; command &…