剑指offer之二叉搜索树和双向链表

简介: 剑指offer之二叉搜索树和双向链表

1 问题

比如我们搜索二叉树如下,我们需要变成双向链表

20170724223902569.png

2 分析

我们知道这个变成双向链接的时候是按照树的中序遍历打印的,我们只需要在中序遍历打印的时候操作该节点,我们可以用临时变量保存这个节点,同时我们也需要单独增加一个链表节点变量,我们需要保证这个节点的左边指向是该链表节点,然后该链表节点的右指向是这个节点,然后我们再把这个节点赋值给这个链表节点,就这样一直移动下去即可。


3 代码实现

                4
           2         6
        1     3   5     7     
#include <stdio.h>
#include <stdlib.h>
typedef struct Tree
{
    int value;
    struct Tree* left;
    struct Tree* right;
} Tree;
/*
 * 把搜索二叉树转成双向链表,我们按照中序遍历
 */
void convertNode(Tree* node, Tree** lastNodeList)
{
    if (node == NULL)
        return;
    Tree* pCurrent = node;
    if (pCurrent->left != NULL)
    {
        convertNode(pCurrent->left, lastNodeList);
    }
    //这里就要进行我们每个节点的前后正确的指向了
    pCurrent->left = *lastNodeList;
    if (*lastNodeList != NULL)
    {
        (*lastNodeList)->right = pCurrent;
    }
    *lastNodeList = pCurrent;
    if (pCurrent->right != NULL)
    {
        convertNode(pCurrent->right, lastNodeList);
    }
}
/*
 * 把翻转好的双向链表尾巴节点移动到链表头
 */
Tree* convert(Tree* node, Tree* lastNodeList)
{
    if (node == NULL || lastNodeList == NULL)
    {
        printf("node is NULL or lastNodeList is NULL\n");
        return NULL;
    }
    Tree* last = NULL;
    convertNode(node, &lastNodeList);
    //因为这个时候lastNodeList已经到了双向链表尾巴,我们需要移动到链表头
    Tree* headNodeList = lastNodeList;
    while (headNodeList != NULL && headNodeList->left != NULL)
    {
        headNodeList = headNodeList -> left;
    } 
    return headNodeList;
}
/*
 * 双向链表从左到右打印
 */
void printRightList(Tree* headNodeList)
{
    if (headNodeList == NULL)
    {
        printf("headNodeList is NULL\n");
        return;
    }
    printf("we will print list from left to right\n");
    Tree* pCurrent = headNodeList;
    while (pCurrent != NULL)
    {
        printf("value is %d\n", pCurrent->value);
        pCurrent = pCurrent->right;
    }
}
/*
 * 双向链表从右到左打印
 */
void printLeftList(Tree* headNodeList)
{
    if (headNodeList == NULL)
    {
        printf("headNodeList is NULL\n");
        return;
    }
    printf("we will print list from right to left\n");
    Tree* pCurrent = headNodeList;
    //先把链表头结点移动为链表的尾巴
    while (pCurrent->right != NULL)
    {
        //printf("value is %d\n", pCurrent->value);
        pCurrent = pCurrent->right;
    }
    //pCurrent = pCurrent->left;
    while (pCurrent != NULL)
    {
        printf("value is %d\n", pCurrent->value);
        pCurrent = pCurrent->left;
    } 
}
int main(void) 
{
    Tree *node1 , *node2 , *node3, *node4, *node5, *node6, *node7;
    node1 = (Tree *)malloc(sizeof(Tree));
    node2 = (Tree *)malloc(sizeof(Tree));
    node3 = (Tree *)malloc(sizeof(Tree));
    node4 = (Tree *)malloc(sizeof(Tree));
    node5 = (Tree *)malloc(sizeof(Tree));
    node6 = (Tree *)malloc(sizeof(Tree));
    node7 = (Tree *)malloc(sizeof(Tree)); 
    node1->value = 4;
    node2->value = 2;
    node3->value = 6;
    node4->value = 1;
    node5->value = 3;
    node6->value = 5;
    node7->value = 7;
    node1->left = node2;
    node1->right = node3;
    node2->left = node4;
    node2->right = node5;
    node3->left = node6;
    node3->right = node7;
    node4->left = NULL;
    node4->right = NULL;
    node5->left = NULL;
    node5->right = NULL;
    node6->left = NULL;
    node6->right = NULL;
    node7->left = NULL;
    node7->right = NULL;
    Tree* list = (Tree *)malloc(sizeof(Tree));
    if (!list)
    {
        printf("malloc list fail\n");
        return -1;
    }
    Tree* firstNodeList = NULL;
    //convertNode(node1, &list);
    firstNodeList = convert(node1, list);
    if (firstNodeList == NULL)
    {
        printf("firstNodeList is NULL\n");
        return -1;
    }
    printRightList(firstNodeList);
    printLeftList(firstNodeList);
    return 0;
}

4 运行结果

we will print list from left to right
value is 0
value is 1
value is 2
value is 3
value is 4
value is 5
value is 6
value is 7
we will print list from right to left
value is 7
value is 6
value is 5
value is 4
value is 3
value is 2
value is 1
value is 0

相关文章
|
6月前
《剑指offer》——合并两个排序的链表
《剑指offer》——合并两个排序的链表
|
6月前
剑指 Offer 35:复杂链表的复制
剑指 Offer 35:复杂链表的复制
48 0
|
1月前
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
46 0
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
52 5
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 18. 删除链表的节点
Leetcode题目"剑指 Offer 18. 删除链表的节点"的Python解决方案,通过使用双指针法找到并删除链表中值为特定数值的节点,然后返回更新后的链表头节点。
41 4
|
6月前
|
Java C语言
剑指offer(牛客)——合并两个排序的链表
剑指offer(牛客)——合并两个排序的链表
39 1
|
6月前
|
存储 Java C语言
剑指offer(牛客)——从尾到头打印链表
剑指offer(牛客)——从尾到头打印链表
41 1
|
6月前
【一刷《剑指Offer》】面试题 17:合并两个排序的链表
【一刷《剑指Offer》】面试题 17:合并两个排序的链表
|
6月前
【一刷《剑指Offer》】面试题 16:反转链表
【一刷《剑指Offer》】面试题 16:反转链表
|
6月前
【一刷《剑指Offer》】面试题 15:链表中倒数第 k 个结点
【一刷《剑指Offer》】面试题 15:链表中倒数第 k 个结点