Tree to text and back
Design a way to store a binary tree as a string. Write encode(root), which turns the tree into a str, and decode(text), which receives only that string and builds an identical tree: same shape, same values. The format is up to you. Values are integers that can be negative, zero, several digits long or repeated, and the tree may be empty. Write both halves yourself; the provided helpers are only for the tests. Each TreeNode has val, left and right.
The tests call the provided run_round_trip(values), which builds the tree from values (level by level with null for a missing child: [1, null, 2, 3] is a root 1 with only a right child 2, whose left child is 3), passes it through encode and then decode, and returns the rebuilt tree in that same format.
Examples
run_round_trip([1,2,3,null,null,4,5])→[1,2,3,null,null,4,5]run_round_trip([])→[]run_round_trip([-7,null,120])→[-7,null,120]