注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
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
复制代码
|