登录
  • #刷题
  • #学python/perl

le‌‌‌‌‌‌‍‍‌‍‍‌‌‍‌‍‌‌‍‌‍‍‍‍‍‍‌‌‍‍‍‌etcode 501题 二叉搜索树 python

Khouhou
318
2
新人发帖

各位大神,有遇到过类似如下情况吗

同一个test case, run code 可以过, 但是submission的时候过不了,比如第leetcode 第501 题:

run code 的时候:

Accepted

Runtime: 20 ms

Your input

[2147483647]

Output

[2147483647]

Expected

[2147483647]

submission 的时候:

Wrong Answer

Details

Input

[2147483647]

Output

[1,2147483647]

Expected

[2147483647]

我怀疑和整数的限制有关, 附上501题我的代码:

# Definition for a binary tree node.

# class TreeNode(object):

# def __init__(self, val=0, left=None, right=None):

# self.val = val

# self.left = left

# self.right = right

class Solution(object):

from collections import defaultdict

pre = None

count = defaultdict(lambda:1)

MAX = 0

def findMode(self, root):

"""

:type root: TreeNode

:rtype: List[int]

"""

self.inorder(root)

ans = [][br]
for k,v in self.count.items():

if v == self.MAX:

ans.append(k)

return ans

def inorder(self,root):

if root == None : return

self.inorder(root.left)

if self.pre == None:

self.MAX = max(self.MAX, self.count[root.val])

elif root.val == self.pre:

self.count[root.val] += 1

self.MAX = max(self.MAX, self.count[root.val])

else:

self.MAX = max(self.MAX, self.count[root.val])

self.pre = root.val

self.inorder(root.right)
2条回复
热度排序

发表回复