首页 > 其他分享 >100. 相同的树

100. 相同的树

时间:2023-01-28 12:11:06浏览次数:41  
标签:None right return val 相同 self 100 left

问题描述

https://leetcode.cn/problems/same-tree/description/

解题思路

相同的树,意思就是树的结构和值都是相同的。

根据这点,可以写出代码。

代码

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

 

标签:None,right,return,val,相同,self,100,left
From: https://www.cnblogs.com/bjfu-vth/p/17070077.html

相关文章