二叉树与链表之间的转换

来源:互联网 发布:java微信开发api文档 编辑:程序博客网 时间:2024/06/07 18:32

一、单链表转换为二叉树

// C++ program to create a Complete Binary tree from its Linked List// Representation#include <iostream>#include <string>#include <queue>using namespace std; // Linked list nodestruct ListNode{    int data;    ListNode* next;}; // Binary tree node structurestruct BinaryTreeNode{    int data;    BinaryTreeNode *left, *right;}; // Function to insert a node at the beginning of the Linked Listvoid push(struct ListNode** head_ref, int new_data){    // allocate node and assign data    struct ListNode* new_node = new ListNode;    new_node->data = new_data;     // link the old list off the new node    new_node->next = (*head_ref);     // move the head to point to the new node    (*head_ref)    = new_node;} // method to create a new binary tree node from the given dataBinaryTreeNode* newBinaryTreeNode(int data){    BinaryTreeNode *temp = new BinaryTreeNode;    temp->data = data;    temp->left = temp->right = NULL;    return temp;} // converts a given linked list representing a complete binary tree into the// linked representation of binary tree.void convertList2Binary(ListNode *head, BinaryTreeNode* &root){    // queue to store the parent nodes    queue<BinaryTreeNode *> q;     // Base Case    if (head == NULL)    {        root = NULL; // Note that root is passed by reference        return;    }     // 1.) The first node is always the root node, and add it to the queue    root = newBinaryTreeNode(head->data);    q.push(root);     // advance the pointer to the next node    head = head->next;     // until the end of linked list is reached, do the following steps    while (head)    {        // 2.a) take the parent node from the q and remove it from q        BinaryTreeNode* parent = q.front();        q.pop();         // 2.c) take next two nodes from the linked list. We will add        // them as children of the current parent node in step 2.b. Push them         // into the queue so that they will be parents to the future nodes        BinaryTreeNode *leftChild = NULL, *rightChild = NULL;        leftChild = newBinaryTreeNode(head->data);        q.push(leftChild);        head = head->next;        if (head)        {            rightChild = newBinaryTreeNode(head->data);            q.push(rightChild);            head = head->next;        }         // 2.b) assign the left and right children of parent        parent->left = leftChild;        parent->right = rightChild;    }} // Utility function to traverse the binary tree after conversionvoid inorderTraversal(BinaryTreeNode* root){    if (root)    {        inorderTraversal( root->left );        cout << root->data << " ";        inorderTraversal( root->right );    }} 


二、二叉树转换为双链表

// Changes left pointers to work as previous pointers in converted DLL// The function simply does inorder traversal of Binary Tree and updates// left pointer using previously visited nodevoid fixPrevPtr(struct node *root){    static struct node *pre = NULL;     if (root != NULL)    {        fixPrevPtr(root->left);        root->left = pre;        pre = root;        fixPrevPtr(root->right);    }} // Changes right pointers to work as next pointers in converted DLLstruct node *fixNextPtr(struct node *root){    struct node *prev = NULL;     // Find the right most node in BT or last node in DLL    while (root && root->right != NULL)        root = root->right;     // Start from the rightmost node, traverse back using left pointers.    // While traversing, change right pointer of nodes.    while (root && root->left != NULL)    {        prev = root;        root = root->left;        root->right = prev;    }     // The leftmost node is head of linked list, return it    return (root);} // The main function that converts BST to DLL and returns head of DLLstruct node *BTToDLL(struct node *root){    // Set the previous pointer    fixPrevPtr(root);     // Set the next pointer and return head of DLL    return fixNextPtr(root);}
剑指offer有另外方法

三、双链表转换为二叉树

0 0
原创粉丝点击