-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumDepthOfBinaryTree.py
More file actions
53 lines (36 loc) · 1.54 KB
/
MaximumDepthOfBinaryTree.py
File metadata and controls
53 lines (36 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 104. Maximum Depth of Binary Tree
# Easy
# 12.3K
# 204
# Companies
# Given the root of a binary tree, return its maximum depth.
# A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
# Example 1:
# Input: root = [3,9,20,null,null,15,7]
# Output: 3
# Example 2:
# Input: root = [1,null,2]
# Output: 2
# Constraints:
# The number of nodes in the tree is in the range [0, 104].
# -100 <= Node.val <= 100
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def maxDepth(self, root):
# Base case: if the root is None, the depth is 0
if root is None:
return 0
# Recursively calculate the depth of the left and right subtrees
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
# The maximum depth is the maximum of the depths of the left and right subtrees, plus 1 for the current node
return max(left_depth, right_depth) + 1
# again for a binary tree we need to use recursion to get to the bottom. for this one we use if root is none return 0.
# we then store our right and left brancehs in the left and rigth depth as we traverse, once we get to the bottom it will
# start returning 0 then will move on to returning the max() of our left and right depths + 1. each time will increase
# by one.