# subject

Given a perfect binary tree, all its leaf nodes are in the same layer, and each parent node has two child nodes. Binary tree is defined as follows:

struct Node {

int val;

Node *left;

Node *right;

Node *next;

}

Fill each of its next pointers so that the pointer points to its next right node. If the next right node cannot be found, set the next pointer to null.

Initially, all next pointers are set to null.

Example:

```
Right: {"$ID": "7", "left": null, "next": null, "right": nul l,"val":7},"val":3},"val":1}
{"$ref": "5"}, "next": null, "right": "{" $ref ":" 6 "}," Val ": 3}," right ":" {"$ ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}
Explanation: given a binary tree as shown in figure a, your function should fill in each of its next pointers to point to its next right node, as shown in Figure B.
```

# Title Solution

## Method 1: sequence traversal

Sequence traversal is used to connect nodes of the same layer;

```
class Solution {
public Node connect(Node root) {
if (root == null) return null;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
Node current = null;
while (size > 0) {
Node node = queue.poll();
if (node.right != null) queue.add(node.right);
if (node.left != null) queue.add(node.left);
node.next = current;
current = node;
size--;
}
}
return root;
}
}
```

## Method 2: recursion

When recursion, we usually decompose it into recursion subproblem and recursion end condition.

**Recursive subproblem**

- Left and right subtrees are connected separately

**Recursion end condition**

- Node = = null, return directly
- Node. Left! = null, connect left.next to node.right
- Node. Right! = null & & node. Next! = null, connect the right of node to the left of node.next. For example, traverse to node 2 and connect 5 to 6

```
class Solution {
public Node connect(Node root) {
// o(1) space.
if (root == null) return null;
if (root.left != null) root.left.next = root.right;
if (root.right != null && root.next != null) root.right.next = root.next.left;
connect(root.left);
connect(root.right);
return root;
}
}
```

## Method 3: sequence traversal o (1) spatial complexity

We used to do sequence traversal with queues before, but sometimes we would require sequence traversal to be solved with constant spatial complexity. The key to this approach is understanding**How to switch from the previous layer to the next layer**。 Dummy is used to record who is the first node of the previous layer and cut to the next layer after traversing one layer

```
class Solution {
public Node connect(Node root) {
Node dummy = new Node(0);
Node pre = dummy;
Node currentRoot = root;
while (currentRoot != null) {
if (currentRoot.left != null) {
pre.next = currentRoot.left;
pre = pre.next;
}
if (currentRoot.right != null) {
pre.next = currentRoot.right;
pre = pre.next;
}
currentRoot = currentRoot.next;
if (currentRoot == null) {
//Switch layers
pre = dummy;
currentRoot = dummy.next;
dummy.next = null;
}
}
return root;
}
}
```

# Popular reading

- Redis realizes distributed lock
- [leetcode] 114. Expand binary tree into linked list
- Summary of social recruitment interview algorithm
- How is the collection type implemented in redis?