-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104.py
52 lines (38 loc) · 1.41 KB
/
104.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
"""
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if root is None:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
def construct_tree(num_list):
all_nodes = [None if num is None else TreeNode(num) for num in num_list]
for idx, node in enumerate(all_nodes):
if node is not None:
left_child_idx = 2 * idx + 1
right_child_idx = 2 * idx + 2
node.left = all_nodes[left_child_idx] if left_child_idx < len(num_list) else None
node.right = all_nodes[right_child_idx] if right_child_idx < len(num_list) else None
return all_nodes[0]
root = construct_tree([3, 9, 20, None, None, 15, 7])
print(Solution().maxDepth(root))