Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def height(node):
- if node is None:
- return 0
- else:
- lDepth = height(node.left)
- rDepth = height(node.right)
- if (lDepth > rDepth):
- return lDepth+1
- else:
- return rDepth+1
- def printLevelorder(tree):
- for d in range(1, height(tree) + 1):
- printGivenLevel(tree, d)
- print()
- def printGivenLevel(tree, level):
- if tree == None:
- return;
- if level == 1:
- print(' '*(countLeftChild(tree)), end='')
- print(tree.data, end='')
- print(' '*(countRightChild(tree)), end='')
- elif level > 1:
- printGivenLevel(tree.left, level-1)
- print(' ', end='')
- printGivenLevel(tree.right, level-1)
- def countLeftChild(root):
- tree = root
- count = 0
- while (tree.left != None):
- count += 1
- tree = tree.left
- return count
- def countRightChild(root):
- tree = root
- count = 0
- while (tree.right != None):
- count += 1
- tree = tree.right
- return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement