leetcode100same tree
来源:互联网 发布:国外翻译的c语言算法书 编辑:程序博客网 时间:2024/06/03 13:05
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
if not p and not q:
return True
elif p and q:
if p.val==q.val:
return self.isSameTree(p.left ,q.left)and self.isSameTree(p.right,q.right)
else:
return False
else:
return False
"""
queue=[[p,q]]
while queue:
pairs=queue.pop(0)
a=pairs[0]
b=pairs[1]
if not a and not b:
continue
elif a and b:
if a.val==b.val:
queue.append([a.left,b.left])
queue.append([a.right,b.right])
else:
return False
else:
return False
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
if not p and not q:
return True
elif p and q:
if p.val==q.val:
return self.isSameTree(p.left ,q.left)and self.isSameTree(p.right,q.right)
else:
return False
else:
return False
"""
queue=[[p,q]]
while queue:
pairs=queue.pop(0)
a=pairs[0]
b=pairs[1]
if not a and not b:
continue
elif a and b:
if a.val==b.val:
queue.append([a.left,b.left])
queue.append([a.right,b.right])
else:
return False
else:
return False
return True
第一种方法:递归
第二种方法:放到一个list里
0 0
- leetcode100same tree
- Tree
- tree
- tree
- TREE
- Tree
- Tree
- tree
- tree
- tree
- tree
- tree
- tree
- Tree
- Tree
- tree
- TREE
- tree
- Linux shell 脚本攻略学习笔记4
- linux下一个网卡配置多个ip【虚拟ip】
- Python 3基础教程15-读文件内容
- Fibonacci数列的递归与非递归实现
- Spring学习五:Bean自动装配
- leetcode100same tree
- Android Icon尺寸标准
- JS创建对象之动态原型模式
- OKHttp原码分析(四)之getResponseWithInterceptorChain方法
- Struts2中 Result的值,加不加斜杠的区别
- 多媒体
- 读书笔记《Effective C++》条款08:别让异常逃离析构函数
- powerDesign 把name自动添加到Commment里
- 文件操作指令