Skip to content

Commit b603602

Browse files
committed
Create 104-max_depth_of_binary_tree.py
1 parent 38430c1 commit b603602

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

104-max_depth_of_binary_tree.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
https://leetcode.com/problems/maximum-depth-of-binary-tree/
3+
4+
Strat:
5+
Do this problem recursively. You might think you need a helper,
6+
but you can do without!
7+
8+
Runtime: O(n) time, O(1) space -- doing a DFS traversal, where you visit every node once + no DS
9+
28 ms 15.6 MB
10+
"""
11+
12+
# Definition for a binary tree node.
13+
# class TreeNode(object):
14+
# def __init__(self, val=0, left=None, right=None):
15+
# self.val = val
16+
# self.left = left
17+
# self.right = right
18+
class Solution(object):
19+
def maxDepth(self, root):
20+
"""
21+
:type root: TreeNode
22+
:rtype: int
23+
"""
24+
if root == None:
25+
return 0
26+
27+
left = self.maxDepth(root.left)
28+
right = self.maxDepth(root.right)
29+
30+
return max(left, right) + 1
31+
32+
### Original thought:
33+
# def maxDepth(self, root):
34+
# """
35+
# :type root: TreeNode
36+
# :rtype: int
37+
# """
38+
# return self.helper(root, 0)
39+
40+
# def helper(self, root, max_depth):
41+
# #base case
42+
# if root == None:
43+
# return max_depth
44+
45+
# #found a known node, increase depth
46+
# max_depth += 1
47+
48+
# left = self.helper(root.left, max_depth)
49+
# right = self.helper(root.right, max_depth)
50+
51+
# return max(left, right)

0 commit comments

Comments
 (0)