Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Mirror Image of a Tree

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Mirror Image of a 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.

You need to log in / sign up to run or submit.

Problem

AMD firmware and driver tools may represent device relationships as binary trees. Given the root of a binary tree, transform it into its mirror image in place by swapping the left and right child of every node, then return the root.

Formal Specification

Implement mirror_tree(root), where root is either None or a TreeNode with fields val, left, and right. Modify the existing nodes rather than creating replacement nodes. The function returns the same root reference, or None for an empty tree.

For examples and test cases, trees are serialized in level-order. null represents a missing child, and unnecessary trailing null values are omitted.

Constraints

  • 0 <= number of nodes <= 10^5
  • -10^9 <= node.val <= 10^9
  • The tree may be balanced or highly skewed
  • The transformation must be performed in place
  • Duplicate node values are allowed

Function Signature

def mirror_tree(root):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output