Python/algorithm

[Leetcode] 2236. Root Equals Sum of Children

baecode 2023. 1. 11. 12:47
반응형

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가 존재하였다.

 

반응형