dsa · easy
Invert Binary Tree
GreyOrangeTreeFoundation
Given the root of a binary tree (represented as a nested list where each node is [value, left, right] or None), invert the tree and return its root.
Arguments
root— the binary tree; each node is[val, left, right]orNone
**Example 1:** `` Input: [4,[2,[1,None,None],[3,None,None]],[7,[6,None,None],[9,None,None]]] Output: [4,[7,[9,None,None],[6,None,None]],[2,[3,None,None],[1,None,None]]] ``
Constraints
0 <= nodes <= 100 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [4,[2,[1,None,None],[3,None,None]],[7,[6,None,None],[9,None,None]]] Expected: [4,[7,[9,null,null],[6,null,null]],[2,[3,null,null],[1,null,null]]]
Example 2
Input: None Expected: null