Sword refers to Offer 36. Binary search tree and doubly linked list (C++) in-order traversal

Enter a binary search tree and convert the binary search tree into a sorted circular doubly linked list. It is required that no new nodes can be created, and only the point of the node pointer in the tree can be adjusted.
In order for you to better understand the problem, take the following binary search tree as an example:
Insert picture description here
we want to convert this binary search tree into a two-way circular linked list. Each node in the linked list has a predecessor and successor pointer. For a doubly circular linked list, the predecessor of the first node is the last node, and the successor of the last node is the first node.

The following figure shows the linked list converted from the above binary search tree. "Head" means pointing to the node with the smallest element in the linked list.

Insert picture description here
In particular, we hope that the conversion operation can be done on-site. When the conversion is completed, the left pointer of the node in the tree needs to point to the predecessor, and the right pointer of the node in the tree needs to point to the successor. You also need to return the pointer of the first node in the linked list.

Note: This question is the same as the main site 426 question: https://leetcode-cn.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/
Note: This question has been changed compared to the original question.

Problem-solving ideas:

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;

    Node() {}

    Node(int _val) {
        val = _val;
        left = NULL;
        right = NULL;
    }

    Node(int _val, Node* _left, Node* _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
    
    
public:
    Node* treeToDoublyList(Node* root) {
    
    
        if(root == nullptr) return nullptr;
        dfs(root);//深度搜索中序遍历树
        head->left = pre;//这两行是接好首尾
        pre->right = head;
        return head;
    }
private:
    Node *pre, *head;
    void dfs(Node* cur) {
    
    
        if(cur == nullptr) return;//当当前节点指向链表的空节点时,返回
        dfs(cur->left);
        if(pre != nullptr) pre->right = cur;//当 pre 不为空时: 修改双向节点引用,即 pre.right = cur , cur.left = pre
        else head = cur;//pre == nullptr时,当 pre 为空时: 代表正在访问链表头节点,记为 head
        cur->left = pre;//这句无论,pre是否为空时,都指向cur的左节点为pre
        pre = cur;//pre指向cur,就是下一轮的cur的前一个节点
        dfs(cur->right);
    }
};

Complexity analysis:

Time complexity O(N): N is the number of nodes in the binary tree, and in-order traversal needs to visit all nodes.
Space complexity O(N): In the worst case, that is, when the tree degenerates to a linked list, the recursion depth reaches N, and the system uses O(N) stack space.

Author: jyd
link: https: //leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/solution/mian-shi-ti-36-er -cha-sou-suo-shu-yu-shuang-xian-5/
Source: LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114945336