Skip to content

Commit 0dbd2df

Browse files
Merge pull request TheAlgorithms#39 from akshaysharma096/master
added isinstance check
2 parents 57be21e + d62e8e2 commit 0dbd2df

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

‎traverals/binary_tree_traversals.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
classTreeNode:
9-
109
def__init__(self, data):
1110
self.data=data
1211
self.right=None
@@ -40,31 +39,32 @@ def build_tree():
4039

4140

4241
defpre_order(node):
43-
ifnotnode:
42+
ifnotisinstance(node, TreeNode) ornotnode:
43+
print("Invalid input")
4444
return
4545
print(node.data, end=" ")
4646
pre_order(node.left)
4747
pre_order(node.right)
4848

4949

5050
defin_order(node):
51-
ifnotnode:
51+
ifnotisinstance(node, TreeNode) ornotnode:
5252
return
5353
in_order(node.left)
5454
print(node.data, end=" ")
5555
in_order(node.right)
5656

5757

5858
defpost_order(node):
59-
ifnotnode:
59+
ifnotisinstance(node, TreeNode) ornotnode:
6060
return
6161
post_order(node.left)
6262
post_order(node.right)
6363
print(node.data, end=" ")
6464

6565

6666
deflevel_order(node):
67-
ifnotnode:
67+
ifnotisinstance(node, TreeNode) ornotnode:
6868
return
6969
q=queue.Queue()
7070
q.put(node)
@@ -79,6 +79,7 @@ def level_order(node):
7979

8080
if__name__=='__main__':
8181
importsys
82+
8283
print("\n********* Binary Tree Traversals ************\n")
8384
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
8485
# otherwise 2.x's input builtin function is too "smart"

0 commit comments

Comments
(0)