Given two binary trees, write a function to verify that they are the same.
If the two trees are structurally identical and the nodes have the same values, they are considered to be the same.
Example 1:
Input: 1 1
/ /\
2 3 2 3
[1,2,3], [1,2,3]
output: True
Example 2:
Input: 1 1
/ \
2 2
[1,2], [1,null,2]
output: false
Example 3:
Input: 1 1
/ /\
2 1 1 2
[1,2,1], [1,1,2]
output: false
Python code
# Definition for a binary tree node. # class TreeNode: # def __init__ (self, x): # self.val = x # Self.left = none # Self.right = None class Solution: def issametree (SE LF, p, Q: "" " : type P:treenode : type Q:treenode : Rtype:bool "" " if not p And not Q: #两二叉树皆为空, recursive boundaries, both NULL returns true return true if p a nd Q and P.val==q.val: L=self.issametree (p.left,q.left) #递归, each time again from the function entrance, Recursive boundary judgment each time R=self.issametree (p.right,q.right) return L and R#and operations require that both L and R be true before returning true. Only the last recursive boundary return value ELSE: &NBSP
Return False