Skip to content

Commit f15601c

Browse files
authored
Update binary_tree_traversals.py
1 parent 5b6f8d8 commit f15601c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

data_structures/binary_tree/binary_tree_traversals.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def preorder(root):
1111
"""
1212
PreOrder traversal: visit root node then its left subtree followed by right subtree.
1313
"""
14-
if(root!=None):
14+
if root:
1515
print(root.data,end=" ")
1616
preorder(root.left)
1717
preorder(root.right)
@@ -20,7 +20,7 @@ def postorder(root):
2020
"""
2121
PostOrder traversal: visit left subtree followed by right subtree and then root node.
2222
"""
23-
if(root!=None):
23+
if root:
2424
postorder(root.left)
2525
postorder(root.right)
2626
print(root.data,end=" ")
@@ -29,7 +29,7 @@ def inorder(root):
2929
"""
3030
InOrder traversal: visit its left subtree followed by root node and then right subtree.
3131
"""
32-
if(root!=None):
32+
if root:
3333
inorder(root.left)
3434
print(root.data,end=" ")
3535
inorder(root.right)
@@ -38,7 +38,7 @@ def height(root):
3838
"""
3939
Recursive function for calculating height of the binary tree.
4040
"""
41-
if(root==None):
41+
if not root:
4242
return 0
4343
leftHeight=height(root.left)
4444
rightHeight=height(root.right)
@@ -52,16 +52,16 @@ def levelorder1(root):
5252
Print whole binary tree in Level Order Traverse.
5353
Level Order traverse: Visit nodes of the tree level-by-level.
5454
"""
55-
if root==None:
55+
if not root:
5656
return
5757
temp=root
5858
que=[temp]
59-
while(len(que)>0):
59+
while len(que)>0:
6060
print(que[0].data,end=" ")
6161
temp=que.pop(0)
62-
if temp.left!=None:
62+
if temp.left:
6363
que.append(temp.left)
64-
if temp.right!=None:
64+
if temp.right:
6565
que.append(temp.right)
6666

6767
def levelorder2(root,level):
@@ -81,7 +81,7 @@ def printlefttoright(root,level):
8181
"""
8282
Print elements on particular level from left to right direction of the binary tree.
8383
"""
84-
if root==None:
84+
if not root:
8585
return
8686
if level==1:
8787
print(root.data,end=" ")
@@ -93,7 +93,7 @@ def printrighttoleft(root,level):
9393
"""
9494
Print elements on particular level from right to left direction of the binary tree.
9595
"""
96-
if root==None:
96+
if not root:
9797
return
9898
if level==1:
9999
print(root.data,end=" ")

0 commit comments

Comments
 (0)