將搜索二叉樹轉換為鏈表_將給定的二叉樹轉換為雙鏈表(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.pswp.cn/news/542546.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542546.shtml
英文地址,請注明出處:http://en.pswp.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 &…