问题描述:
# 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: TreeNode, q: TreeNode) -> bool: if not p and not q:return True if not p or not q:return False a=[] def pre_order(root): a.append(root.val) if root.left: pre_order(root.left) else: a.append(None) if root.right: pre_order(root.right) else: a.append(None) return a stack1=pre_order(p) a=[] stack2=pre_order(q) return True if stack1==stack2 else False
思路:就是用先序遍历的手段 效果还不错
我是小郑 期待与你一起进步😀