LeetCode: Invert Binary Tree Solution

A Broken Interview: The Origin

Approach

Classic swap solution

1temp = a
2a = b
3b = temp

Recursively call the tree branches

Implementation

1var invertTree = function (root) {
2 if (!root) return root
3 let temp = root.left
4 root.left = root.right
5 root.right = temp
6 invertTree(root.left)
7 invertTree(root.right)
8 return root
9}

References

Original problem

"you cant invert a binary tree on a whiteboard so fuck off"

Comments

Loading comments...

Tags

leetcode

recursion

tree

binary tree

Next Post

LeetCode: Balanced Binary Tree

Sep 3, 2022

Modified version of "Maximum Depth of Binary Tree"

Previous Post

HoningJS

Search Posts