100. 相同的树

100. 相同的树

  • 题目-简单难度
  • 示例
  • 1. dfs
  • 2. bfs

题目-简单难度

给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例

示例 1:
100. 相同的树_第1张图片

输入:p = [1,2,3], q = [1,2,3]
输出:true

示例 2:
100. 相同的树_第2张图片

输入:p = [1,2], q = [1,null,2]
输出:false

示例 3:
100. 相同的树_第3张图片

输入:p = [1,2,1], q = [1,1,2]
输出:false

提示:

  • 两棵树上的节点数目都在范围 [0, 100] 内
  • -104 <= Node.val <= 104

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/summary-ranges
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1. dfs

时间
24ms
击败 99.95%使用 Python3 的用户
内存
15.72mb
击败 47.06%使用 Python3 的用户

# 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 or q == None: return p == q
        # 递归对比两树的节点值和左右节点是否相同
        return p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

2. bfs

时间
44ms
击败 57.39%使用 Python3 的用户
内存
15.79mb
击败 17.39%使用 Python3 的用户

# 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 not p and not q:
            return True
        l1 = [p]
        l2 = [q]
        while l1 and l2:
            # 转换为val列表进行对比
            ll1 = [i.val for i in l1 if i]
            ll2 = [i.val for i in l2 if i]
            # 若值不同,返回False
            if ll1 != ll2:
                return False
            # 遍历
            for _ in range(len(l1)):
                # 获取节点
                n1 = l1.pop(0)
                n2 = l2.pop(0)
                # 如果节点对应不上,返回False
                if (not n1.left) ^ (not n2.left) or (not n1.right) ^ (not n2.right):
                    return False
                # 判断并添加入l1和l2列表,方便遍历后续节点
                if n1.left != None:
                    l1.append(n1.left)
                if n2.left != None:
                    l2.append(n2.left)
                if n1.right != None:
                    l1.append(n1.right)
                if n2.right != None:
                    l2.append(n2.right)
        # 只有在l1和l2两个列表都为空的情况下,返回True
        return not l1 and not l2

Or

# 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 not p and not q:
            return True
        l1 = [p]
        l2 = [q]
        while l1 and l2:
            # 遍历
            for _ in range(len(l1)):
                # 获取节点
                n1 = l1.pop(0)
                n2 = l2.pop(0)
                if (n1 and not n2) or (n2 and not n1):
                    return False
                if n1.val != n2.val:
                    return False
                # 如果节点对应不上,返回False
                if (not n1.left) ^ (not n2.left) or (not n1.right) ^ (not n2.right):
                    return False
                # 判断并添加入l1和l2列表,方便遍历后续节点
                if n1.left != None:
                    l1.append(n1.left)
                if n2.left != None:
                    l2.append(n2.left)
                if n1.right != None:
                    l1.append(n1.right)
                if n2.right != None:
                    l2.append(n2.right)
        # 只有在l1和l2两个列表都为空的情况下,返回True
        return not l1 and not l2

你可能感兴趣的:(二叉树,算法,python,算法,树,二叉树,leetcode)