Skip to content

Commit 432a272

Browse files
solves invert a binary tree in python
1 parent 9a6604e commit 432a272

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ContainsDuplicate.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/contains_duplicate.py) |
6565
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ContainsDuplicateII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/contains_duplicate_ii.py) |
6666
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyStack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/implement_stack_using_queues.py) |
67-
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/InvertBinaryTree.java) |
67+
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/InvertBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/invert_binary_tree.py) |
6868
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PowerOf2.java) |
6969
| 232 | [Implement Queue Using Stacks]() | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) |
7070
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeLinkedList.java) |

Diff for: python/invert_binary_tree.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def invertTree(self, root: TreeNode) -> Optional[TreeNode]:
14+
if root is None:
15+
return None
16+
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
17+
return root

0 commit comments

Comments
 (0)