Reverse Odd Levels of Binary Tree - LeetCode
머 이런 문제가 다 있냐? 휴
리트코드라는 사이트에 아직 익숙하지 않아서 어떻게 풀으라는 건지 좀 감이 안 왔다.
LeetCode 2415 Reverse odd levels of Binary Tree Python Solution (youtube.com)
이 유튜브의 설명을 참고해서 공부해가면서 풀어 보았다.
DFS로 재귀함수를 계속 호출해서 푸는 방법이다.
class Solution(object):
def reverseOddLevels(self, root):
"""
:type root: Optional[TreeNode]
:rtype: Optional[TreeNode]
"""
def helper(root1, root2, depth):
if root1 is None and root2 is None:
return None
if depth % 2 != 0:
tmp = root1.val
root1.val = root2.val
root2.val = tmp
helper(root1.left, root2.right, depth +1)
helper(root1.right, root2.left, depth +1)
helper(root.left, root.right, 1)
return root
'알고리즘 PS > Sorting' 카테고리의 다른 글
1011. Capacity To Ship Packages Within D Days - 이분탐색 문제 (0) | 2024.06.11 |
---|---|
프로그래머스 입국심사 - 이분탐색 문제 (0) | 2024.06.10 |
정렬 알고리즘의 시간 복잡도와 기수 정렬 (0) | 2024.05.26 |
파이썬으로 힙정렬 구현하기 (max_heapify와 heap_sort함수) (0) | 2024.03.31 |
정렬 알고리즘 (선택 정렬, 삽입 정렬, 퀵 정렬, 계수 정렬) with python (0) | 2024.03.31 |