LeetCode icon indicating copy to clipboard operation
LeetCode copied to clipboard

Using get function of dictionary

Open lokendra1704 opened this issue 5 years ago • 1 comments

lokendra1704 avatar Jul 14 '20 19:07 lokendra1704

I tried to submit new code with .get() function on LeetCode and faced a Runtime Error

AttributeError: 'NoneType' object has no attribute 'append'
    queue[depth+1] = queue.get(depth+1,[]).append(node.right)
Line 23 in levelOrder (Solution.py)
    ret = Solution().levelOrder(param_1)
Line 43 in _driver (Solution.py)
    _driver()
Line 54 in <module> (Solution.py)

but after I replaced

if node.left:
    queue[depth+1] = queue.get(depth+1,[]).append(node.left)

with

if node.left:
    queue[depth+1] = queue.get(depth+1,[])
    queue[depth+1].append(node.left)

It works fine.

YuriSpiridonov avatar Jul 15 '20 11:07 YuriSpiridonov