반응형
Problem
이번 문제는 이진 트리를 주고 ? 루트, 왼쪽, 오른쪽 자식 값이 존재한다.
left , right 의 합이 root와 같으면 True 아니면 false를 return 한다.
나의 풀이
# 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 checkTree(self, root: Optional[TreeNode]) -> bool:
#print(root.left, root.left.val)
#print(root.right, root.right.val)
#print(root)
return root.val == root.left.val + root.right.val
문제자체는 어려운 게 없었다.
다만 개념을 학습시키는 문제 같았던것이 print를 찍었을때 각 수가 모두 트리노드객체로 생성되어서 val,left,right가 존재하였다.
반응형
'Python > algorithm' 카테고리의 다른 글
에라토스테네스의 체 [소수 구하기] (0) | 2025.03.31 |
---|---|
[Leetcode] 1528. Shuffle String (1) | 2023.01.12 |
[Leetcode] 1365. How Many Numbers Are Smaller Than the Current Number (1) | 2023.01.05 |
[Leetcode] 1281. Subtract the Product and Sum of Digits of an Integer (2) | 2023.01.04 |
[Leetcode] 1431. Kids With the Greatest Number of Candies (1) | 2023.01.02 |