#2 most askedVery commonEasyTree DFS
Invert Binary Tree
New to Tree DFS? Learn it from scratch firstTrees & Binary Search Trees β
π’ Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.Mirror a binary tree (swap every node's left and right children) and return the root.
[4, 2, 7, 1, 3, 6, 9] β [4, 7, 2, 9, 6, 3, 1]
π§ͺ Practice
Write your solution, then press Run tests. Explain your approach out loud as you go.
def invert_tree(root):
pass
Output appears here. Press Run or Ctrl/β + Enter.
Try it first. Struggling for a few minutes is how it sticks.