Implement in-order traversal using a stack, optimize it, and explain time and space complexity.
Asked in the Technical 2 stage.
Implement inorder_traversal(root). The input is a binary tree represented by nested dictionaries with keys "val", "left", and "right"; an empty child is None. Return a list of node values in left-root-right order. Use an iterative stack-based approach rather than recursion.
Example 1: {"val": 2, "left": {"val": 1, "left": null, "right": null}, "right": {"val": 3, "left": null, "right": null}} returns [1, 2, 3].
Example 2: None returns [].
Constraints: at most 10^4 nodes, values are integers, and the tree may be skewed.
def inorder_traversal(root):