比较两棵树是否相同(关键词:树/二叉树/相同/相等/完全相同)

版权声明:本文为博主原创文章,可以转载,但转载前请联系博主。 https://blog.csdn.net/qq_33528613/article/details/84930277

比较两棵树是否相同

实现

def isSameTree(p, q):
	if p == None and q == None:
		return True
	elif p != None and q != None:
		return p.val == q.val and isSameTree(p.left, q.left) and isSameTree(p.right, q.right)
	else:
		return False

参考文献:

  1. https://leetcode.com/problems/same-tree/submissions/;
  2. 这是印象笔记中的笔记,如果是在CSDN手机APP上查看此博客,请在印象笔记手机APP中搜索该参考文献:https://app.yinxiang.com/shard/s44/nl/9329661/67840819-0c82-47a8-a4ba-ba87fbc26584;
  3. 19 求两棵树是否相同

猜你喜欢

转载自blog.csdn.net/qq_33528613/article/details/84930277