判断二叉树是否相等(leetcode-100)

  • 递归依据:两棵树相同位置节点的子树也必须相等
  • 递归出口:两个节点全是None,相等;只有一个是None,不等;两个都不是None,但节点值不相等,不等。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
    @classmethod
    def isSameTree(self, p, q):
        if p == None and q == None:
            return True
        elif p == None or q == None:
            return False
        elif p.val != q.val:
            return False
        """ :type p: TreeNode :type q: TreeNode :rtype: bool """
        return (Solution.isSameTree(p.left, q.left) and Solution.isSameTree(p.right, q.right))
    原文作者:满二叉树
    原文地址: https://blog.csdn.net/u013632190/article/details/51966899
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞