View
#Binary_Tree #Recursion #BFS #DFS
URL
Intuition
- Recursion 사용
Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
root.left, root.right = root.right, root.left
root.left = self.invertTree(root.left)
root.right = self.invertTree(root.right)
return root
.
'CS > Coding Test' 카테고리의 다른 글
[LeetCode] 704. Binary Search (0) | 2023.07.20 |
---|---|
[LeetCode] 242. Valid Anagram (0) | 2023.07.20 |
[LeetCode] 125. Valid Palindrome, 4가지 solution 성능 비교 (0) | 2023.07.20 |
[LeetCode] 121.Best Time to Buy and Sell Stock, Time Limit Exceeded (0) | 2023.07.17 |
[LeetCode] 21.Merge Two Sorted Lists (0) | 2023.07.17 |
reply