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

Commit 0e8efb0

Browse files
python: add problem 103 and unittest
1 parent 2fb7ee5 commit 0e8efb0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Python/sln_101_200/solution_101_110.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ def recursive_tree(level, node):
5555
recursive_tree(0, root)
5656
return results
5757

58+
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
59+
"""
60+
103
61+
:param root:
62+
:return:
63+
"""
64+
if not root:
65+
return []
66+
node_list = [root]
67+
next_node_list = []
68+
zigzag_list = []
69+
idx = 0
70+
71+
while node_list:
72+
zigzag = []
73+
for node in node_list:
74+
if idx//2:
75+
zigzag.append(node.val)
76+
else:
77+
zigzag.insert(0, node.val)
78+
if node.left:
79+
next_node_list.append(node.left)
80+
if node.right:
81+
next_node_list.append(node.right)
82+
83+
node_list = next_node_list
84+
next_node_list = []
85+
zigzag_list.append(zigzag)
86+
idx += 1
87+
88+
return zigzag_list
89+
5890
def maxDepth(self, root):
5991
"""
6092
104
@@ -69,6 +101,13 @@ def maxDepth(self, root):
69101

70102
return left_depth if left_depth > right_depth else right_depth
71103

104+
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
105+
"""
106+
107
107+
:param root:
108+
:return:
109+
"""
110+
72111
def isBalanced(self, root: TreeNode) -> bool:
73112
"""
74113
110

Python/sln_101_200/test_solution_101_110.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from unittest import TestCase
33
from sln_101_200.solution_101_110 import Solution_101_110, TreeNode
4+
from common_utils import build_binary_tree
45

56

67
class TestSolution_101_110(TestCase):
@@ -21,3 +22,7 @@ def test_maxDepth(self):
2122
root.right = right_1
2223
depth = self.sln.maxDepth(root)
2324
self.assertEqual(depth, 4)
25+
26+
def test_zigzagLevelOrder(self):
27+
tree = build_binary_tree([3, 9, 20, None, None, 15, 7])
28+
self.assertEqual(self.sln.zigzagLevelOrder(tree), [[3], [20, 9], [15, 7]])

0 commit comments

Comments
 (0)