Your question is Populate Next Right Pointers. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Nykaa's category and recommendation surfaces can represent hierarchical content as a binary tree. Given the root of a binary tree, populate each node's next pointer so it points to the node immediately to its right on the same level. The rightmost node at every level must point to None.
Return the root after modifying the tree in place. The TreeNode class has fields val, left, right, and next; all next pointers are initially None. Do not create a separate queue or list proportional to the tree size.
root, a TreeNode or None.next pointers populated.null for missing children. Output is represented as the values encountered by following next pointers from the first node of each level.next pointers are initially Nonedef connect(root):