【LeetCode-中等】剑指 Offer 36. 二叉搜索树与双向链表

题目链接

剑指 Offer 36. 二叉搜索树与双向链表

标签

后序遍历、二叉搜索树

步骤

  • 二叉搜索树中的任一节点的直接前驱为其左子树的最右侧节点,直接后继为其右子树的最左侧节点。因此,可以通过这个关系来操作原来的二叉树。
  • 为了不影响深度较大的节点的判断,使用后序遍历。

Step1. 后序遍历,寻找 root 的最左侧和最右侧节点,分别设为 head, tail。其中,head 是整棵树最左侧的节点,相当于中序遍历中最先输出的节点。

void postOrder(Node* root) {
    if (root == nullptr) {
        return;
    }
    postOrder(root->left);
    if (!findHead && root->left == nullptr) { // 设置头结点
        head = root;
        findHead = true;
    }
    postOrder(root->right);
    // 找左子树的最右侧节点: 直接前驱
    Node *rightest = findRightest(root->left);
    if (rightest != nullptr) {
        root->left = rightest;
        rightest->right = root;
    }
    // 找右子树的最左侧节点:直接后继
    Node *leftest = findLeftest(root->right);
    if (leftest == nullptr) {
        tail = root;
    } else {
        root->right = leftest;
        leftest->left = root;
    }
}

Step2. 补充寻找指定节点左子树最右侧、右子树最右侧的节点的代码。

/* 	
	Node *leftest  = findLeftest(root->right),
		 *rightest = findRightest(root->left);
 */
Node* findRightest(Node* root) { // 找以root为根的二叉树中,最右侧的节点
    if (root == nullptr) {
        return nullptr;
    }
    while (root->right != nullptr) {
        root = root->right;
    }
    return root;
}
Node* findLeftest(Node* root) {
    if (root == nullptr) {
        return nullptr;
    }
    while (root->left != nullptr) {
        root = root->left;
    }
    return root;
}

Step3. 构建 headtail 之间的联系。

head->left = tail;
tail->right = head;

实现代码(C++)

class Solution {
public:
    Node *head, *tail;
    bool findHead = false;
    Node* findRightest(Node* root) {
        if (root == nullptr) {
            return nullptr;
        }
        while (root->right != nullptr) {
            root = root->right;
        }
        return root;
    }
    Node* findLeftest(Node* root) {
        if (root == nullptr) {
            return nullptr;
        }
        while (root->left != nullptr) {
            root = root->left;
        }
        return root;
    }
    void postOrder(Node* root) {
        if (root == nullptr) {
            return;
        }
        postOrder(root->left);
        if (!findHead && root->left == nullptr) { // 设置头结点
            head = root;
            findHead = true;
        }
        postOrder(root->right);
        // 找左子树的最右侧节点: 直接前驱
        Node *rightest = findRightest(root->left);
        if (rightest != nullptr) {
            root->left = rightest;
            rightest->right = root;
        }
        // 找右子树的最左侧节点:直接后继
        Node *leftest = findLeftest(root->right);
        if (leftest == nullptr) {
            tail = root;
        } else {
            root->right = leftest;
            leftest->left = root;
        }
    }
    Node* treeToDoublyList(Node* root) {
        if (root == nullptr) {
            return nullptr;
        }
        postOrder(root);
        head->left = tail;
        tail->right = head;
        return head;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值