LeetCode Entry
226. Invert Binary Tree
Let's write a recursive one-liner.
fun invertTree(root: TreeNode?): TreeNode? =
root?.apply { left = invertTree(right).also { right = invertTree(left) } }
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/122
Intuition
Walk tree with Depth-First Search and swap each left and right nodes.
Approach
Let’s write a recursive one-liner.
Complexity
- Time complexity: \(O(n)\)
- Space complexity: \(O(log_2(n))\)