When I was running a case in the "Test Case" Console, I got accepted, but when submitting the result, I failed to pass on that case. I have attached my code and the image that describes the issue.
My code:
def getLonelyNodes(self, root, arr=[]):
"""
:type root: TreeNode
:rtype: List[int]
"""
### 用Recursive
## 特殊情况:
# 1. Base Case: 如果root没有node,return arr ?;
if (root.left is None) and (root.right is None):
return arr
# 2. 如果root有一个node,将此node的val加入arr,并recurse该node
elif (root.left is not None) and (root.right is None):
arr += [root.left.val]
self.getLonelyNodes(root.left, arr)
elif (root.left is None) and (root.right is not None):
arr += [root.right.val]
self.getLonelyNodes(root.right, arr)
# 3. 如果root有两个nodes,分别recurse两个node
else:
self.getLonelyNodes(root.left, arr)
self.getLonelyNodes(root.right, arr)
return arr