0100-相同的树


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

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

示例 1:

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

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

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

提示:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/same-tree

python

# 0100.相同的树

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        """
        DFS
        :param p:
        :param q:
        :return:
        """
        # p,q空,True
        if not p and not q:
            return True
        # 一者空,Flase
        elif not p or not q:
            return False
        # 值不等,False
        elif p.val != q.val:
            return False
        # 左右子树递归
        else:
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

class Solution2:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q:
            return True
        if not p or not q:
            return False

        from collections import deque
        queue1 = deque([p])
        queue2 = deque([q])

        while queue1 and queue2:
            node1 = queue1.popleft()
            node2 = queue2.popleft()

            if node1.val != node2.val:
                return False
            left1, right1 = node1.left, node1.right
            left2, right2 = node2.left, node2.right

            if (left1  and not left2) or (not left1 and left2):
                return False
            if (right1 and not right2) or (not right1 and right2):
                return False


            if left1:
                queue1.append(left1)
            if right1:
                queue1.append(right1)
            if left2:
                queue2.append(left2)
            if right2:
                queue2.append(right2)
        # 遍历完,都为空队列,一定相同树
        return not queue1 and queue2

golang

package binaryTree

import "container/list"

// DFS
func isSameTree(p,q *TreeNode) bool {
	if p == nil && q == nil {
		return true
	}else if p == nil || q == nil {
		return false
	}else if p.Val != q.Val {
		return false
	} else {
		return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
	}
}

// BFS
func isSameTree2(p,q *TreeNode) bool {
	if p == nil && q == nil {
		return true
	}
	if p == nil || q == nil {
		return false
	}
	queue1 := list.New()
	queue1.PushBack(p)
	queue2 := list.New()
	queue2.PushBack(q)

	for queue1.Len() >0 && queue2.Len() > 0 {
		node1 := queue1.Remove(queue1.Front()).(*TreeNode)
		node2 := queue2.Remove(queue2.Front()).(*TreeNode)

		if node1.Val != node2.Val {
			return false
		}
		left1, right1 := node1.Left, node1.Right
		left2, right2 := node2.Left, node2.Right

		if (left1 != nil && left2 == nil) || (left1 == nil && left2 != nil) {
			return false
		}
		if (right1 != nil && right2== nil) || (right1 == nil && right2 != nil) {
			return false
		}

		if left1 != nil {
			queue1.PushBack(left1)
		}
		if right1 != nil {
			queue1.PushBack(right1)
		}
		if left2 != nil {
			queue2.PushBack(left2)
		}
		if right2 != nil {
			queue2.PushBack(right2)
		}
	}
	return queue1.Len()==0 && queue2.Len()==0
}