ยท 1 min read
Blind 75 - Invert Binary Tree
Swap left and right. Then repeat for those nodes.
[Video]
[Problem explanation in English]
Intuition: Swap left and right. Then repeat for those nodes.
2 lines can be saved by using a single temporary variable instead of left & right, but this is clean.
O(n) time. O(1) space
public TreeNode invertTree(TreeNode root) {
if(root == null){
return root;
}
TreeNode left = root.right;
TreeNode right = root.left;
root.right = right;
root.left = left;
invertTree(left);
invertTree(right);
return root;
}Related posts
- Blind 75 - Maximum Depth of Binary Tree Nov 16, 2022
- Blind 75 - Linked List Cycle Nov 16, 2022
- Blind 75 - Merge Two-Sorted Lists Nov 16, 2022
Get new posts by email
No spam. Unsubscribe anytime. Or grab the RSS feed.
Mailto-based for now. ButtonDown / ConvertKit can replace this form later.