무냐의 개발일지
BST: Invert Binary Tree 본문
👩💻 문제
BST: Invert Binary Tree ( ** Interview Question)
Objective: Write a method to invert (or mirror) a binary tree. This means that for every node in the binary tree, you will swap its left and right children.
💡 풀이
def invert(self):
self.root = self.__invert_tree(self.root)
def __invert_tree(self, current):
if current == None:
return None
left = current.left
current.left = self.__invert_tree(current.right)
current.right = self.__invert_tree(left)
return current
✍️ 해설
쉽게 풀었다. 왼쪽 노드와 오른쪽 노드를 바꿔주기만 하면 됨!!
🥳 배운점
할 수 이 따 ! !
'LeetCode 코딩테스트' 카테고리의 다른 글
[기본개념] BST 기본 구조 (0) | 2024.07.01 |
---|---|
BST: Convert Sorted List to Balanced BST (0) | 2024.07.01 |
HT: Find Duplicates (1) | 2024.06.19 |
DLL: Swap Nodes in Pairs (Advanced!) (0) | 2024.06.18 |
Stack : sort_stack 해설 (0) | 2024.06.17 |