Last active
April 4, 2022 15:01
-
-
Save ThomasHigginson/030667cefc5fa8e1fc53db7aa36f5937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Iterative DFS solution using a stack | |
def maxDepth(self, root: Optional[TreeNode]) -> int: | |
if not root: | |
return 0 | |
stack = [(root, 1)] # (Node, depth of Node) | |
maxDepth = 1 | |
while stack: | |
node, depth = stack.pop() | |
maxDepth = max(maxDepth, depth) | |
if node.left: | |
stack.append((node.left, depth+1)) | |
if node.right: | |
stack.append((node.right, depth+1)) | |
return maxDepth | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment