-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoot_values_binary_tree
More file actions
38 lines (38 loc) · 1.08 KB
/
Copy pathRoot_values_binary_tree
File metadata and controls
38 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Node:
def __init__(self,key):
self.left=None
self.right=None
self.value=key
class BinaryTree:
def __init__(self):
self.root1=None
#used to the tree from class Node and its object root
def _inorder_recursive(self,node):
if node:
self._inorder_recursive(node.left)
print(node.value,end="->")
self._inorder_recursive(node.right)
def print_leaves(self,node):
#print all leaf nodes of binary tree
if node is None:
return
if node.left is None and node.right is None:
print(node.value,end='')
if node.left:
self.print_leaves(node.left)
if node.right:
self.print_leaves(node.right)
#Create nodes
root=Node(10)
root.left=Node(34)
root.right=Node(20)
root.left.left=Node(3)
root.left.right=Node(4)
root.right.left=Node(12)
root.right.right=Node(54)
tree=BinaryTree()
tree.root1=root
print(tree._inorder_recursive(tree.root1))
#print the leaf nodes
print("Leaf nodes of binary tree:",end=' ')
tree.print_leaves(tree.root1)