Node Depths

Tiffany ·2024년 3월 8일
0

AlgoExpert

목록 보기
10/20

def nodeDepths(root):
    # Write your code here.
    stack = [(root, 0)]
    finalDepth = 0 
    while stack:
        node, depth = stack.pop() 
        finalDepth += depth 
        if node.left:
            stack.append([node.left, depth+1])
        if node.right:
            stack.append([node.right, depth+1])
    return finalDepth  
        
        
profile
Love what you do and don't quit.

0개의 댓글