Given a perfect binary tree where all leaves are at the same depth and every parent node has exactly two children, update each nodes next pointer to point to its immediate right neighbor at the same level. If there is no right neighbor, set the next pointer to None. Return the root node after updating all next pointers. The input is provided as a tree structure. The output should be the root node with updated next pointers. For testing, serialize the tree in level order using the next pointers, with #' marking the end of each level.
Example 1
Input: TreeNode(10, TreeNode(5, TreeNode(2), TreeNode(8)), TreeNode(15, TreeNode(12), TreeNode(20)))
Output: [10,#,5,15,#,2,8,12,20,#]
Explanation: Level order traversal using next pointers: 10,#, 5,15,#, 2,8,12,20,#
Example 2
Input: TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6), TreeNode(7)))
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Level order traversal using next pointers: 1,#, 2,3,#, 4,5,6,7,#
Example 3
Input: None
Output: []
Explanation: Empty tree returns empty list.
Constraints
Case 1
Input: TreeNode(100, TreeNode(50, TreeNode(25), TreeNode(75)), TreeNode(150, TreeNode(125), TreeNode(175)))
Expected: [100,#,50,150,#,25,75,125,175,#]
Case 2
Input: TreeNode(42, TreeNode(21, TreeNode(11), TreeNode(31)), TreeNode(63, TreeNode(53), TreeNode(73)))
Expected: [42,#,21,63,#,11,31,53,73,#]
Case 3
Input: TreeNode(7, TreeNode(3, TreeNode(1), TreeNode(5)), TreeNode(11, TreeNode(9), TreeNode(13)))
Expected: [7,#,3,11,#,1,5,9,13,#]