Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Observer Pattern
- BubbleSort
- KAKAO
- easy
- 백준
- golang
- Singleton Pattern
- k8s
- 피보나치
- kubernetes
- Python
- Top-down
- docker
- Kotlin
- 그리디
- 파이썬
- github
- go
- LeetCode
- cpu scheduling
- Programmers
- GCP
- Dynamic Programming
- Backjoon
- GKE
- java
- Codility
- mobaXTerm
- 알고리즘
Archives
- Today
- Total
To Be Developer
[LeetCode] 965. Univalued Binary Tree (Python) 본문
https://leetcode.com/problems/univalued-binary-tree/
class Solution(object):
def isUnivalTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
# TreeNode 가 존재하지 않을 때 True
if not root:
return True
# root의 left 가 존재하고 root의 val 과 root의 left의 val 값이 다르면
# return False
if root.left and root.val != root.left.val:
return False
# root의 right 가 존재하고 root의 val과 root의 right의 val 값이 다르면
# return False
if root.right and root.val != root.right.val:
return False
# root의 left와 right의 val이 root의 val 과 같다면
# left right를 완전 탐색하여 True 이면 True
# False 이면 return False
return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)
'알고리즘 > LeetCode' 카테고리의 다른 글
| [LeetCode] 62. Unique Paths (GoLang, Python) (0) | 2019.04.12 |
|---|---|
| [LeetCode] Climbing Stairs (GoLang, Python) (0) | 2019.04.11 |
| [LeetCode] 509. Fibonacci Number (GoLang, Python) (0) | 2019.04.07 |
| [LeetCode] Monotonic Array (Python) (0) | 2019.04.05 |
| [LeetCode] Find the Difference (Python) (0) | 2019.04.03 |