Mirror a binary tree and analyze complexity
Palo Alto NetworksSoftware Engineer · OnsiteLast updated
This question evaluates understanding of binary tree manipulation and traversal methods, emphasizing recursion and iterative approaches as well as the ability to reason about time and space complexity.
Given the root of a binary tree, convert it to its mirror by swapping every node’s left and right children. Implement both recursive and iterative (using a stack or queue) solutions, analyze time and space complexity, and discuss edge cases (empty tree, single node, highly unbalanced). Perform a dry run on this tree (level order): [5,3,8,null,4,7,9].
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.