- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheightBinaryTree.py
More file actions
Latest commit
33 lines (26 loc) · 838 Bytes
/
Copy pathheightBinaryTree.py
File metadata and controls
33 lines (26 loc) · 838 Bytes
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
classNode(object):
def__init__(self, value):
self.value=value
self.left=None
self.right=None
classBinaryTree(object):
def__init__(self, root):
self.root=Node(root)
defheight(self, node):
ifnodeisNone:
return0
leftHeight=self.height(node.left)
rightHeight=self.height(node.right)
return1+max(leftHeight, rightHeight)
tree=BinaryTree(2)
tree.root.left=Node(3)
tree.root.right=Node(4)
tree.root.left.left=Node(5)
tree.root.left.right=Node(6)
tree.root.left.left.left=Node(9)
tree.root.right.left=Node(7)
tree.root.right.right=Node(8)
tree.root.right.left.left=Node(1)
tree.root.right.left.right=Node(2)
tree.root.right.right.right=Node(8)
print(tree.height(tree.root))