Hook

Their other posts in the index, biggest breakout first.
If you are new to LeetCode, you need to know this problem. Invert Binary Tree. So given the root of a binary tree, invert the tree and return its root. Think about it and I'll do the code. Okay, so we're gonna do this recursion, and our base case is gonna be if the root is None, then we're just gonna return. So if root is None, then we return. Now we have to swap them. So we could use a temporary variable, just to make it easier. So root.right, we want root.right to be equal to root.left. So we're swapping root.right and root.left. And then because we just changed root.right, we actually need root.left to be equal to temporary. So temp. Now we only swapped the root.right and root.left, but in root.right and root.left, swapped, and we invert those. So we have to call the function on itself. So this is where the recursion part comes in. We're gonna call self.invertTree on root.right, and we're gonna call it on root.left. So self.invertTree(root.right). Then if we just return the root, it's gonna be the inverted tree, and we're done.