DIY: Populating Next Right Pointers in Each Node II
Solve the interview question "Populating Next Right Pointers in Each Node II" in this lesson.
We'll cover the following
You are given a perfect binary tree. We have added an additional
next pointer to our
next pointer so it points to its next right node. If there is no next right node, the
next pointer should be set to null.
next pointers are set to null.
The input will be a perfect binary tree, and you will be provided with its root node. The following is an example input:
3 / \ 9 20
The output will be the root node with every
next pointer connected. For the above input, the output will be:
3 - null / \ 9--20 - null
For this coding exercise, you need to implement the
traverse(root) function, where
root is the root node of the binary tree. The function should return the same root node with every
next pointer properly connected.