# Share my simple Java Recursive Solution

• Nothing fancy, just level order traversal, we are Spurs! We play fundamental basketball!

``````public void connect(TreeLinkNode root) {
if (root == null) return;

traverse(list);
}

if (list.size() == 0) return;

for (int i = 0; i < list.size(); i++) {
if (i != list.size() - 1)  {
node.next = list.get(i + 1);
}

}
traverse(next);
}``````

• Your code is not constant space. You store nodes for each layer. Space complexity should be O(logn).

But I think you could modify to achieve that. Use the next pointer to traversal through a layer instead of extra List.

• can you explain a little more about how to lower the space complexity by "Use the next pointer to traversal through a layer instead of extra List"?

• Recursion here is not necessary. Since you use ArrayList, your code can be improved by using while or for loop.

• Notice this. You use list to store all nodes in current layer in order to traversal them and then next layer. Notice the next pointer is actually linking each node in current layer. If you want to traversal current layer, call node.next like you traversal a linked list.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.