728x90
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
if p is None and q is None:
return True
if p is not None and q is not None:
return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
return False
출처: github.com/jiapengwen/LeetCode/blob/master/Python/same-tree.py
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Maximum Depth of Binary Tree 파이썬 (recursion) (0) | 2021.03.31 |
---|---|
[LeetCode] Symmetric Tree 파이썬 (iterative, recursion) (0) | 2021.03.31 |
[LeetCode] Merge Sorted Array 파이썬 (modify in-place) (0) | 2021.03.31 |
[LeetCode] Remove Duplicates from Sorted List 파이썬 (Linked List, shallow copy, deepcopy) (0) | 2021.03.31 |
[LeetCode] Climbing Stairs 파이썬 (DP) (0) | 2021.03.30 |
댓글