| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 코드업
- graph
- 생성형AI
- tree
- sql코테
- codeup
- SQL
- 파이썬
- Stack
- 슬라이딩윈도우
- 알고리즘
- two-pointer
- GenAI
- 니트코드
- array
- stratascratch
- nlp
- 릿코드
- 리트코드
- 투포인터
- binary Tree
- 파이썬알고리즘
- Python
- Python3
- GenerativeAI
- Greedy
- heap
- dfs
- BFS
- LeetCode
Archives
- Today
- Total
Tech for good
[Leetcode/Tree, Binary Tree, DFS] 572. Subtree of Another Tree 본문
IT/Computer Science
[Leetcode/Tree, Binary Tree, DFS] 572. Subtree of Another Tree
Diana Kang 2025. 7. 26. 04:54

# 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 isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not root:
return False
return self.sameTree(root, subRoot) or self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)
def sameTree(self, root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
return root1.val == root2.val and self.sameTree(root1.left, root2.left) and self.sameTree(root1.right, root2.right)'IT > Computer Science' 카테고리의 다른 글
| [Leetcode/Array, Hash Table, Graph] 997. Find the Town Judge (0) | 2025.08.08 |
|---|---|
| [HackerRank] Inorder Traversal of Binary Tree (0) | 2025.08.04 |
| [Leetcode/Tree, Binary Search Tree, DFS] 1305. All Elements in Two Binary Search Trees (2) | 2025.07.26 |
| [Leetcode/Tree, Binary Tree, DFS] 110. Balanced Binary Tree (0) | 2025.07.25 |
| [Leetcode/Tree, DFS, Binary Tree] 543. Diameter of Binary Tree (0) | 2025.07.25 |