# Print even and odd level nodes in binary tree in python Python program for Print even and odd level nodes in binary tree. Here mentioned other language solution.

``````#  Python 3 program for
#  Print the nodes of even and odd levels of binary tree

#  Binary Tree node
class TreeNode :
def __init__(self, data) :
#  Set node value
self.data = data
self.left = None
self.right = None

class BinaryTree :
def __init__(self) :
self.root = None

#  Print all nodes which is exist in odd Level
def oddLevel(self, node, level) :
if (node != None) :
if (level % 2 != 0) :
#  Display even odd node value
print("", node.data, end = " ")

self.oddLevel(node.left, level + 1)
self.oddLevel(node.right, level + 1)

#  Print all nodes which is exist in even Level
def evenLevel(self, node, level) :
if (node != None) :
if (level % 2 == 0) :
#  Display even level node value
print("", node.data, end = " ")

self.evenLevel(node.left, level + 1)
self.evenLevel(node.right, level + 1)

def main() :
#  Create new tree
tree = BinaryTree()
#    Binary Tree
#    -------------
#        10
#       /  \
#      2    4
#     /    / \
#    3    6   5
#     \    \
#      9    7
#            \
#             11
tree.root = TreeNode(10)
tree.root.left = TreeNode(2)
tree.root.left.left = TreeNode(3)
tree.root.left.left.right = TreeNode(9)
tree.root.right = TreeNode(4)
tree.root.right.right = TreeNode(5)
tree.root.right.left = TreeNode(6)
tree.root.right.left.right = TreeNode(7)
tree.root.right.left.right.right = TreeNode(11)
print("\n Even Level Nodes ", end = ":")
tree.evenLevel(tree.root, 1)
print("\n Odd Level Nodes  ", end = ":")
tree.oddLevel(tree.root, 1)

if __name__ == "__main__": main()``````

Output

`````` Even Level Nodes : 2  9  4  7
Odd Level Nodes  : 10  3  6  11  5``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.