Your question is Mirror a Binary Tree. 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.
Fireblocks policy evaluation can represent approval rules as a binary tree. Given the root of a binary policy tree, mirror the tree in place by recursively swapping the left and right children of every node, then return the original root.
The operation must reuse the existing TreeNode objects. Do not create replacement nodes or copy the tree. The interviewer may also ask how your approach behaves when the tree is highly unbalanced.
Assume each node has the following interface:
node.val: an integer rule identifiernode.left: a TreeNode or Nonenode.right: a TreeNode or NoneImplement mirror_policy_tree(root), where root is a TreeNode or None. Return the same root object after mutating the entire tree. Test inputs and expected outputs use nested objects with val, left, and right fields to represent TreeNode instances.
def mirror_policy_tree(root):