-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path104-max_depth_of_binary_tree.py
51 lines (41 loc) · 1.29 KB
/
104-max_depth_of_binary_tree.py
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
"""
https://leetcode.com/problems/maximum-depth-of-binary-tree/
Strat:
Do this problem recursively. You might think you need a helper,
but you can do without!
Runtime: O(n) time, O(1) space -- doing a DFS traversal, where you visit every node once + no DS
28 ms 15.6 MB
"""
# 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):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
left = self.maxDepth(root.left)
right = self.maxDepth(root.right)
return max(left, right) + 1
### Original thought:
# def maxDepth(self, root):
# """
# :type root: TreeNode
# :rtype: int
# """
# return self.helper(root, 0)
# def helper(self, root, max_depth):
# #base case
# if root == None:
# return max_depth
# #found a known node, increase depth
# max_depth += 1
# left = self.helper(root.left, max_depth)
# right = self.helper(root.right, max_depth)
# return max(left, right)