Skip to content

Commit 8b85222

Browse files
committed
Update invert-a-binary-tree.md
1 parent 95b84ad commit 8b85222

1 file changed

Lines changed: 0 additions & 15 deletions

File tree

articles/invert-a-binary-tree.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,21 +1370,6 @@ impl Solution {
13701370

13711371
Forgetting to check for a null root causes null pointer exceptions. Always return null immediately if the root is null.
13721372

1373-
### Swapping After Recursive Calls
1374-
1375-
If you swap children after making recursive calls, you end up swapping already-inverted subtrees back. The swap must happen before or the recursion will undo the inversion.
1376-
1377-
```python
1378-
# Wrong: swaps after recursion undoes the inversion
1379-
self.invertTree(root.left)
1380-
self.invertTree(root.right)
1381-
root.left, root.right = root.right, root.left
1382-
# Correct: swap first, then recurse
1383-
root.left, root.right = root.right, root.left
1384-
self.invertTree(root.left)
1385-
self.invertTree(root.right)
1386-
```
1387-
13881373
### Using Wrong References After Swap
13891374

13901375
After swapping, `root.left` now points to what was previously `root.right`. When recursing, make sure you're using the correct references for the swapped children.

0 commit comments

Comments
 (0)