二叉搜索树与双向链表

题目描述

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

代码

class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        TreeNode* lastNode = NULL;
        convert(pRootOfTree, &lastNode);
        while (lastNode != NULL && lastNode->left != NULL) {
            lastNode = lastNode->left;
        };
        return lastNode;
    }

    void convert(TreeNode* root, TreeNode** lastNode)//中序遍历就是排序后的结果
    {
        if (root == NULL) {
            return;
        }

        //左节点
        if (root->left != NULL) {
            convert(root->left, lastNode);
        }
        root->left = *lastNode;
        if (*lastNode != NULL) {
            (*lastNode)->right = root;
        }

        *lastNode = root;

        //右节点
        if (root->right != NULL) {
            convert(root->right, lastNode);
        }
    }
};
原文地址:https://www.cnblogs.com/jecyhw/p/6560617.html