Skip to content

added isinstance check #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions traverals/binary_tree_traversals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class TreeNode:

def __init__(self, data):
self.data = data
self.right = None
Expand Down Expand Up @@ -40,31 +39,32 @@ 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)
pre_order(node.right)


def in_order(node):
if not node:
if not isinstance(node, TreeNode) or not node:
return
in_order(node.left)
print(node.data, end=" ")
in_order(node.right)


def post_order(node):
if not node:
if not isinstance(node, TreeNode) or not node:
return
post_order(node.left)
post_order(node.right)
print(node.data, end=" ")


def level_order(node):
if not node:
if not isinstance(node, TreeNode) or not node:
return
q = queue.Queue()
q.put(node)
Expand All @@ -79,6 +79,7 @@ def level_order(node):

if __name__ == '__main__':
import sys

print("\n********* Binary Tree Traversals ************\n")
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
# otherwise 2.x's input builtin function is too "smart"
Expand Down