r/LeetcodeChallenge • • 7h ago

STREAK🔥🔥🔥 LeetCode: 226. Invert Binary Tree

Post image

Inverting a binary tree moves entire subtrees with a single pointer swap. Nothing is copied. Mirror the left subtree, mirror the right, swap them. The tree rearranges itself in place.

The swap is the part worth picturing: one assignment moves an entire subtree with all its children.

Bonus question: can you swap before recursing instead of after? Yes. What you can't do is recurse left, swap, then recurse right.

Why does recurse-left, swap, recurse-right break it?

Day 85/365 of my LeetCode challenge: 226. Invert Binary Tree. Problem link and full breakdown in today's newsletter.

https://www.linkedin.com/pulse/leetcode-invert-binary-tree-archit-agarwal-dm6tc

#LeetCode #DSA #BinaryTree #Golang

1 Upvotes

0 comments sorted by