相同的树

在这里插入图片描述
先进行节点的添加,然后进行节点的值的判断,注意初始根节点可能为None,并且最后的时候2个数组的长度还不一样,也需要判断。

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        arr1=[]
        arr2=[]
        if p==None and q==None:
            return True
        elif p==None or q==None:
            return False

        arr1.append(p)
        arr2.append(q)

        while(len(arr1)!=0 and len(arr2)!=0):
            temp1=arr1.pop(0)
            temp2=arr2.pop(0)
            if temp1.val!=temp2.val:#对于值的判断在这里进行编码
                return False

            #先添加节点
            if temp1.left!=None and temp2.left!=None:
                arr1.append(temp1.left)
                arr2.append(temp2.left)
            elif temp1.left==None and temp2.left==None:
                pass
            else:
                return False

            if temp1.right!=None and temp2.right!=None:
                arr1.append(temp1.right)
                arr2.append(temp2.right)
            elif temp1.right==None and temp2.right==None:
                pass
            else:
                return False
        if len(arr1)==0 and len(arr2)==0:
            return True
        else:
            return False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值