-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104.py
More file actions
71 lines (52 loc) · 1.68 KB
/
104.py
File metadata and controls
71 lines (52 loc) · 1.68 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest
path from the root node down to the farthest leaf node.
'''
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
return self.maxDepthH(root, 0)
def maxDepthH(self, root, h):
if not root:
return h
# if root.left and not root.right:
# return self.maxDepthH(root.right, h + 1)
# elif root.right and not root.left:
# return self.maxDepthH(root.left, h + 1)
# else:
return max(self.maxDepthH(root.right, h + 1), self.maxDepthH(root.left, h + 1))
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
height = {root:0,}
level = [root]
while level:
for node in level:
if node not in height.keys():
height[node] = 1
if node.left:
height[node.left] = height[node] + 1
if node.right:
height[node.right] = height[node] + 1
nxt = [(node.left, node.right) for node in level]
level = [nd for nodeLR in nxt for nd in nodeLR if nd]
h = max([h for h in height.values()])
print(h)
# print(list((x.val, y) for x, y in height.items()))
return h + 1