File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ def main():
7373 node = lst[4] # get node
7474
7575 print('Saved node:', node.value)
76+ # Output: Saved node: 3
7677
7778 lst.insert(4, 4) # insert new value into list
7879 lst.append(2) # append a new value
@@ -83,17 +84,18 @@ def main():
8384 lst.reverse() # reverse list
8485
8586 print('List length:', len(lst)) # length of the list
87+ # Output: List length: 9
8688
89+ # iteration over values
8790 for val in lst.values():
88- # iteration over values
8991 print(val, end=' ')
9092
9193 # Output: 1 2 3 4 5 6 7 8 9
9294
9395 print('')
9496
97+ # iteration over nodes
9598 for node in lst:
96- # iteration over nodes
9799 print(node.value, end=' ')
98100
99101 # Output: 1 2 3 4 5 6 7 8 9
@@ -157,13 +159,9 @@ def generate_tree() -> TreeNode:
157159def dfs(root: Optional[TreeNode]) -> Generator[int, None, None]:
158160 \" ""Depth-first search.\" ""
159161 if root:
160- if root.left:
161- yield from dfs(root.left)
162-
162+ yield from dfs(root.left)
163163 yield root.value
164-
165- if root.right:
166- yield from dfs(root.right)
164+ yield from dfs(root.right)
167165
168166
169167def main():
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ def main():
1717 node = lst[4] # get node
1818
1919 print('Saved node:', node.value)
20+ # Output: Saved node: 3
2021
2122 lst.insert(4, 4) # insert new value into list
2223 lst.append(2) # append a new value
@@ -27,17 +28,18 @@ def main():
2728 lst.reverse() # reverse list
2829
2930 print('List length:', len(lst)) # length of the list
31+ # Output: List length: 9
3032
33+ # iteration over values
3134 for val in lst.values():
32- # iteration over values
3335 print(val, end=' ')
3436
3537 # Output: 1 2 3 4 5 6 7 8 9
3638
3739 print('')
3840
41+ # iteration over nodes
3942 for node in lst:
40- # iteration over nodes
4143 print(node.value, end=' ')
4244
4345 # Output: 1 2 3 4 5 6 7 8 9
Original file line number Diff line number Diff line change @@ -65,13 +65,9 @@ def generate_tree() -> TreeNode:
6565def dfs(root: Optional[TreeNode]) -> Generator[int, None, None]:
6666 \" ""Depth-first search.\" ""
6767 if root:
68- if root.left:
69- yield from dfs(root.left)
70-
68+ yield from dfs(root.left)
7169 yield root.value
72-
73- if root.right:
74- yield from dfs(root.right)
70+ yield from dfs(root.right)
7571
7672
7773def main():
You can’t perform that action at this time.
0 commit comments