파비의 매일매일 공부기록

2023.03.13 Today's Challenge 본문

Problem Solving/LeetCode

2023.03.13 Today's Challenge

fabichoi 2023. 3. 13. 23:45

https://leetcode.com/problems/symmetric-tree/

 

Symmetric Tree - LeetCode

Can you solve this real interview question? Symmetric Tree - Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).   Example 1: [https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg] Input: roo

leetcode.com

class Solution:
    def isMirror(self, left, right):
        if not left and not right:
            return True
        if not left or not right:
            return False
        return left.val == right.val and self.isMirror(left.left, right.right) and self.isMirror(left.right, right.left)
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        return self.isMirror(root.left, root.right)
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.03.15 Today's Challenge  (0) 2023.03.15
2023.03.14 Today's Challenge  (0) 2023.03.14
2023.03.12 Today's Challenge  (0) 2023.03.12
2023.03.11 Today's Challenge  (0) 2023.03.11
2023.03.10 Today's Challenge  (0) 2023.03.10
Comments