日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将搜索二叉树转换为链表_将给定的二叉树转换为双链表(DLL) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

將搜索二叉樹轉換為鏈表

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

給定二叉樹,我們必須將其轉換為雙鏈表(DLL)。

Algorithm:

算法:

To solve the problem we can follow this algorithm:

為了解決這個問題,我們可以遵循以下算法:

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

    我們開始從最右邊的樹節點到最左邊的樹節點將樹節點轉換為DLL。

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

    每次我們將節點的右指針連接到DLL的頭時。

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

    將DLL的左指針連接到該節點。

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

    使該節點位于鏈接列表的開頭。

  • 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

    將搜索二叉樹轉換為鏈表

    總結

    以上是生活随笔為你收集整理的将搜索二叉树转换为链表_将给定的二叉树转换为双链表(DLL)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。