# What does leetcode mean

Time：2021-8-19

Enter a binary search tree and convert the binary search tree into a sorted circular two-way linked list. It is required that no new node can be created, and only the node pointer in the tree can be adjusted.

To better understand the problem, take the following binary search tree as an example:

We hope to transform this binary search tree into a two-way circular linked list. Each node in the linked list has a precursor and successor pointer. For a two-way circular linked list, the precursor 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 transformed from the above binary search tree“ “Head” refers to the node with the smallest element in the linked list.

In particular, we want the conversion to be done in place. After the conversion is completed, the left pointer of the node in the tree needs to point to the precursor 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.

Train of thought analysis
Because it is a binary search tree, the middle order traversal of the binary search tree is a process from small to large. We can traverse in the middle orderWebpage GameIn the process of processing the two-way circular linked list, first connect the head node and the tail node. Pre is used to record the node on the left side of cur in the two-way linked list, that is, cur in the last iteration. When pre = = null, there is no node on the left side of cur, that is, cur is the head node in the two-way linked list; Conversely, pre= When null, there is a node pre on the left side of cur, and the operation of pre. Right = cur is required. Whether pre is null or not has no effect on this sentence, and this sentence can be placed before the above two if else sentences.

Code display
Solution 1:

``````public Node treeToDoublyList(Node root) {
if(root==null) return null;
dfs(root);
head.left =pre;// The order of these two sentences can also be reversed by pointing to each other between the head node and the tail node
}
public void dfs(Node cur){
if(cur==null) return;
dfs(cur.left);
//Pre is used to record the node on the left side of cur in the two-way linked list, that is, cur in the last iteration. When pre = = null, there is no node on the left side of cur, that is, cur is the head node in the two-way linked list
//Conversely, pre= When null, there is a node pre on the left side of cur, and the operation of pre. Right = cur is required.
else pre.right = cur;

cur.left = pre;// Whether pre is null or not has no effect on this sentence, and this sentence can be placed before the above two if else sentences.
pre = cur;// Pre points to the current cur
dfs(cur.right);// After all iterations are completed, pre points to the tail node in the bidirectional linked list
}``````

Click and drag to move
Linked list of specific depth nodes (interview questions)
Title Description
Given a binary tree, design an algorithm to create a linked list containing all nodes at a certain depth (for example, if the depth of a tree is D, D linked lists will be created). Returns an array of linked lists containing all depths.

Example 1:

Input: [1,2,3,4,5, null, 7,8]

``````    1
/  \ 11
2    3
/ \    \
4   5    7``````

/
8
Output: , [2,3], [4,5,7], ]
Click and drag to move
Train of thought analysis
In fact, this problem can be understood as sequence traversal. If the depth of a tree is D, D linked lists will be created, equivalent to a two-dimensional array, and then traversed layer by layer.

Code display
Solution 1:

public ListNode[] listOfDepth(TreeNodewww.sangpi.com tree) {

``````if(tree == null) return null;
List<ListNode> list = new ArrayList<>();
Deque<TreeNode> que = new ArrayDeque<>();
while(!que.isEmpty()){
int n = que.size();
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
for(int i=0;i<n;i++){
TreeNode curTree = que.removeFirst();
cur.next = new ListNode(curTree.val);// Generate new node
cur = cur.next;// Move backward