Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d29a3c4

Browse files
python: add problem 102 and unittest
1 parent 19bf709 commit d29a3c4

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import math
3+
from typing import List
4+
5+
36
class TreeNode:
47
def __init__(self, x):
58
self.val = x
@@ -8,6 +11,29 @@ def __init__(self, x):
811

912

1013
class Solution_101_110(object):
14+
def levelOrder(self, root: TreeNode) -> List[List[int]]:
15+
"""
16+
102
17+
:param root:
18+
:return:
19+
"""
20+
results = []
21+
22+
def recursive_tree(level, node):
23+
if not node:
24+
return
25+
if len(results) == level:
26+
level_list = [node.val]
27+
results.append(level_list)
28+
else:
29+
results[level].append(node.val)
30+
31+
recursive_tree(level + 1, node.left)
32+
recursive_tree(level + 1, node.right)
33+
34+
recursive_tree(0, root)
35+
return results
36+
1137
def maxDepth(self, root):
1238
"""
1339
104
@@ -17,7 +43,7 @@ def maxDepth(self, root):
1743
if not root:
1844
return 0
1945

20-
left_depth = self.maxDepth(root.left)+1
21-
right_depth = self.maxDepth(root.right)+1
46+
left_depth = self.maxDepth(root.left) + 1
47+
right_depth = self.maxDepth(root.right) + 1
2248

23-
return left_depth if left_depth>right_depth else right_depth
49+
return left_depth if left_depth > right_depth else right_depth

0 commit comments

Comments
 (0)