中级农民
积分 105
大米 颗
鳄梨 个
水井 尺
蓝莓 颗
萝卜 根
小米 粒
学分 个
注册时间 2017-11-4
最后登录 1970-1-1
注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
在leetcode 始终无法通过,但是在自己本地EDI中编译测试都没问题请问大家有什么建议
AttributeError: 'int' object has no attribute 'val'
Line 88 in _driver ( Solution.py )
Line 96 in ( Solution.py )
# 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 lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
p_path = self.find_path(root, p)
q_path = self.find_path(root, q)
for num in p_path[::-1]:
if num in q_path:
return num
return -1
def find_path(self, root, p):
if not root:
return []
root_stack = [root]
root_path = [[root.val]]
rep = []
while root_stack:
len_root_stack = len(root_stack)
for i in range(len_root_stack):
cur_root = root_stack.pop(0)
cur_root_path = root_path.pop(0)
if cur_root == p:
rep = cur_root_path
root_stack = []
return rep
break
else:
if cur_root.left:
root_stack.append(cur_root.left)
root_path.append(cur_root_path+[cur_root.left.val])
if cur_root.right:
root_stack.append(cur_root.right)
root_path.append(cur_root_path+[cur_root.right.val])
return rep 复制代码
上一篇:
刷题经验和讲解整理 下一篇:
solution通过了,但效率不高。