Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0dbd2df

Browse files
authoredOct 14, 2016
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

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

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

4140

4241
def pre_order(node):
43-
if not node:
42+
if not isinstance(node, TreeNode) or not node:
43+
print("Invalid input")
4444
return
4545
print(node.data, end=" ")
4646
pre_order(node.left)
4747
pre_order(node.right)
4848

4949

5050
def in_order(node):
51-
if not node:
51+
if not isinstance(node, TreeNode) or not node:
5252
return
5353
in_order(node.left)
5454
print(node.data, end=" ")
5555
in_order(node.right)
5656

5757

5858
def post_order(node):
59-
if not node:
59+
if not isinstance(node, TreeNode) or not node:
6060
return
6161
post_order(node.left)
6262
post_order(node.right)
6363
print(node.data, end=" ")
6464

6565

6666
def level_order(node):
67-
if not node:
67+
if not isinstance(node, TreeNode) or not node:
6868
return
6969
q = queue.Queue()
7070
q.put(node)
@@ -79,6 +79,7 @@ def level_order(node):
7979

8080
if __name__ == '__main__':
8181
import sys
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)
Please sign in to comment.