프로그래밍/LeetCode
[LeetCode][Python3] 100. Same Tree
snoopybox
2018. 9. 26. 02:13
Problem :
https://leetcode.com/problems/same-tree/description/
My Solution :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if (p and q) is None:
return p is q
return (p.val == q.val and
self.isSameTree(p.left, q.left) and
self.isSameTree(p.right, q.right))
Comment :
주어진 2개의 binary tree가 동일한지 확인하는 문제이다. recursion으로 접근하였다.
그나저나 Python에서 80자 이내로 개행하는거 너무 어렵다. and로 연결된 3가지 조건을 어떻게 표현하는게 가독성이 제일 좋은지 모르겠다.