-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104 Maximum Depth of Binary Tree.py
55 lines (44 loc) · 1.54 KB
/
104 Maximum 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
52
53
54
55
import json
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
# Shrink the long json string by removing unnecessary lines
t = '\n'.join([l for l in self.json_repr.split('\n')
if '[' not in l and ']' not in l])
return t.replace(',','')
@property
def json_repr(self):
if self.val is None:
return 'None'
# Recursively construct a json-compliant representation
if self.left is None:
text = f"[{self.val}]"
else:
text = f"[{self.val}, [{self.left.json_repr}, {self.right.json_repr}]]"
return json.dumps(json.loads(text), indent=1)
def from_list(l):
nodes = [TreeNode(v) for v in l]
kids = nodes[::-1]
root = kids.pop()
for node in nodes:
if node:
if kids:
node.left = kids.pop()
if kids:
node.right = kids.pop()
return root
root1 = [3,9,20,None,None,15,7]
root2 = [1,None,2]
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
return max(left_depth,right_depth) + 1
test = Solution()
print(test.maxDepth(TreeNode.from_list(root1)))
print(test.maxDepth(TreeNode.from_list(root2)))