File tree 1 file changed +16
-6
lines changed
data_structures/binary_tree
1 file changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,19 @@ def inorder(root: Node | None) -> list[int]:
58
58
return [* inorder (root .left ), root .data , * inorder (root .right )] if root else []
59
59
60
60
61
+ def reverse_inorder (root : Node | None ) -> list [int ]:
62
+ """
63
+ Reverse in-order traversal visits right subtree, root node, left subtree.
64
+ >>> reverse_inorder(make_tree())
65
+ [3, 1, 5, 2, 4]
66
+ """
67
+ return (
68
+ [* reverse_inorder (root .right ), root .data , * reverse_inorder (root .left )]
69
+ if root
70
+ else []
71
+ )
72
+
73
+
61
74
def height (root : Node | None ) -> int :
62
75
"""
63
76
Recursive function for calculating the height of the binary tree.
@@ -161,15 +174,12 @@ def zigzag(root: Node | None) -> Sequence[Node | None] | list[Any]:
161
174
162
175
163
176
def main () -> None : # Main function for testing.
164
- """
165
- Create binary tree.
166
- """
177
+ # Create binary tree.
167
178
root = make_tree ()
168
- """
169
- All Traversals of the binary are as follows:
170
- """
171
179
180
+ # All Traversals of the binary are as follows:
172
181
print (f"In-order Traversal: { inorder (root )} " )
182
+ print (f"Reverse In-order Traversal: { reverse_inorder (root )} " )
173
183
print (f"Pre-order Traversal: { preorder (root )} " )
174
184
print (f"Post-order Traversal: { postorder (root )} " , "\n " )
175
185
You can’t perform that action at this time.
0 commit comments