just 4 lines | very easy for beginners | beats 100% 
IntuitionIn a perfect binary tree:
All levels are completely filled.
Every parent node has exactly two children.
The goal is to populate the next pointers of ea
# Intuition
In a perfect binary tree:
- All levels are completely filled.
- Every parent node has exactly two children.
  
The goal is to populate the `next` pointers of each node to point to its immediate right neighbor. If there is no right neighbor, the `next` pointer should point to `null`.

We can take advantage of the tree's perfect structure to recursively connect nodes.

# Approach
1. **Base Case**:  
   - If the root is `null`, we simply return `null`.

2. **Recursive Connection**:  
   - Connect the left child of the current node to its right child:  
     `root.left.next = root.right`
   - If the right child has a valid neighbor (via the `next` pointer of the current node), connect it to the left child of that neighbor:  
     `root.right.next = root.next.left`
   - Recursively perform the above steps for the left and right subtrees.

3. **Recursive Traversal**:  
   - Traverse the tree depth-first, connecting nodes level by level.

# Complexity
- **Time Complexity**:  
  $$O(n)$$  
  Each node is visited once, making the traversal linear in the number of nodes.
  
- **Space Complexity**:  
  $$O(h)$$  
  The recursion stack depends on the height of the tree, which is $$O(\log(n))$$ for a perfect binary tree.

# Java Code
```java 
class Solution {
    public Node connect(Node root) {
        // Base case: If the tree is empty, return null
        if (root == null) return null;
        
        // Connect the left child to the right child
        if (root.left != null) root.left.next = root.right;
        
        // Connect the right child to the left child of the next node, if it exists
        if (root.right != null && root.next != null) root.right.next = root.next.left;
        
        // Recursively connect the left and right subtrees
        connect(root.left);
        connect(root.right);
        
        // Return the root after connections are made
        return root;
    }
}
```
![image.png](https://assets.leetcode.com/users/images/a31d3e17-2a7b-43a7-add1-8fb75b4b0fb3_1737904446.7171812.png)


['Tree', 'Recursion', 'Binary Tree', 'Java']