-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHeightOfABinaryTree.py
44 lines (34 loc) · 922 Bytes
/
HeightOfABinaryTree.py
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
39
40
41
42
43
44
"""
@author Anirudh Sharma
Given a binary tree, find its height.
Constraints:
1 <= Number of nodes <= 10^5
1 <= Data of a node <= 10^5
"""
def getHeight(root):
# Special case
if root is None:
return 0
# Get the height of the left subtree
leftHeight = getHeight(root.left)
# Get the height of the right subtree
rightHeight = getHeight(root.right)
# Return the max of left subtree height
# and right subtree height plus 1 for root
return 1 + max(leftHeight, rightHeight)
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
if __name__ == "__main__":
root = Node(1)
root.left = Node(3)
root.right = Node(2)
print(getHeight(root))
root = Node(10)
root.left = Node(20)
root.right = Node(30)
root.left.left = Node(40)
root.left.right = Node(60)
print(getHeight(root))