LeetCode 100.相同的树(简单)

python 递归

# 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        if(p==None and q==None):
            return True
        
        if(p==None or q==None):
            return False
        
        if(p.val !=q.val):
            return False
        
        return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
        
        

LeetCode 100.相同的树(简单)_第1张图片

 

你可能感兴趣的:(LeetCode刷题,leetcode,算法,职场和发展)