I am trying to solve a problem that requires finding the minimum depth of a binary tree. I can't seem to find the error. Any advice would be of great help.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
queue = []
queue = collections.deque(queue)
queue.append(root)
depth = 0
while len(queue) != 0:
numberOfNodes = len(queue)
while numberOfNodes > 0:
currentNode = queue.popleft()
if not currentNode and not currentNode:
depth += 1
return depth
if currentNode.left:
queue.append(currentNode.left)
if currentNode.right:
queue.append(currentNode.right)
numberOfNodes -= 1
depth += 1
return depth
if not currentNode and not currentNode:a typo? I think it should beif not currentNode.left and not currentNode.right: