LeetCode第 426 题:将二叉搜索树转化为排序的双向链表(C++)

    科技2022-08-18  101

    426. 将二叉搜索树转化为排序的双向链表

    一样的剑指 Offer 36. 二叉搜索树与双向链表_zj-博客,中序遍历一边遍历一边调整指针

    class Solution { public: //left, right对应前驱、后继 Node *head = NULL, *cur = NULL;//当前节点,上一个节点 void inorder(Node* root){//中序遍历 if(root->left) inorder(root->left); if(!head){ head = root; cur = root; }else{ cur->right = root; root->left = cur; cur = root; } if(root->right) inorder(root->right); } Node* treeToDoublyList(Node* root) { if(!root) return NULL; inorder(root); head->left = cur;//首尾相连 cur->right = head; return head; } };
    Processed: 0.015, SQL: 9