mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
Merge pull request #39 from akshaysharma096/master
added isinstance check
This commit is contained in:
commit
0dbd2df11b
|
@ -6,7 +6,6 @@ import queue
|
||||||
|
|
||||||
|
|
||||||
class TreeNode:
|
class TreeNode:
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.right = None
|
self.right = None
|
||||||
|
@ -40,7 +39,8 @@ def build_tree():
|
||||||
|
|
||||||
|
|
||||||
def pre_order(node):
|
def pre_order(node):
|
||||||
if not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
|
print("Invalid input")
|
||||||
return
|
return
|
||||||
print(node.data, end=" ")
|
print(node.data, end=" ")
|
||||||
pre_order(node.left)
|
pre_order(node.left)
|
||||||
|
@ -48,7 +48,7 @@ def pre_order(node):
|
||||||
|
|
||||||
|
|
||||||
def in_order(node):
|
def in_order(node):
|
||||||
if not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
in_order(node.left)
|
in_order(node.left)
|
||||||
print(node.data, end=" ")
|
print(node.data, end=" ")
|
||||||
|
@ -56,7 +56,7 @@ def in_order(node):
|
||||||
|
|
||||||
|
|
||||||
def post_order(node):
|
def post_order(node):
|
||||||
if not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
post_order(node.left)
|
post_order(node.left)
|
||||||
post_order(node.right)
|
post_order(node.right)
|
||||||
|
@ -64,7 +64,7 @@ def post_order(node):
|
||||||
|
|
||||||
|
|
||||||
def level_order(node):
|
def level_order(node):
|
||||||
if not node:
|
if not isinstance(node, TreeNode) or not node:
|
||||||
return
|
return
|
||||||
q = queue.Queue()
|
q = queue.Queue()
|
||||||
q.put(node)
|
q.put(node)
|
||||||
|
@ -79,6 +79,7 @@ def level_order(node):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
print("\n********* Binary Tree Traversals ************\n")
|
print("\n********* Binary Tree Traversals ************\n")
|
||||||
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
|
# 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"
|
# otherwise 2.x's input builtin function is too "smart"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user