From 887f9e5b01c36ae3fb24355046c78ab3aee390dd Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Fri, 14 Oct 2016 00:01:44 +0530 Subject: [PATCH] added isinstance check --- traverals/binary_tree_traversals.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/traverals/binary_tree_traversals.py b/traverals/binary_tree_traversals.py index c1e8783b2..6b0dd5ee9 100644 --- a/traverals/binary_tree_traversals.py +++ b/traverals/binary_tree_traversals.py @@ -40,7 +40,8 @@ def build_tree(): def pre_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return print(node.data, end=" ") pre_order(node.left) @@ -48,7 +49,8 @@ def pre_order(node): def in_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return in_order(node.left) print(node.data, end=" ") @@ -56,7 +58,8 @@ def in_order(node): def post_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return post_order(node.left) post_order(node.right) @@ -64,7 +67,8 @@ def post_order(node): def level_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return q = queue.Queue() q.put(node)