forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepthBST.py
More file actions
67 lines (64 loc) · 1.81 KB
/
depthBST.py
File metadata and controls
67 lines (64 loc) · 1.81 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Node:
def __init__(self,data):
self.right=self.left=None
self.data = data
class Solution:
def insert(self,root,data):
if root==None:
return Node(data)
else:
if data<=root.data:
cur=self.insert(root.left,data)
root.left=cur
else:
cur=self.insert(root.right,data)
root.right=cur
return root
def getHeight(self,root):
#Write your code here
# Misses the path route over the middle
"""
height_right = 0
height_left = 0
right = root
left = root
while right.right:
right = right.right
height_right += 1
while left.left:
left = left.left
height_left += 1
if height_left > height_right:
return height_left
else:
return height_right
###
"""
### Leaf Logic would be --> if both left.left and left.right are None
# Recursive is the best solution
"""
if root is None:
return -1
else:
#print root.data
left_height = self.getHeight(root.left)
right_height = self.getHeight(root.right)
#print left_height, right_height
if left_height > right_height:
return left_height+1
else:
return right_height+1
"""
# Recursive + One Line SOlution
if root is None:
return -1
else:
return 1+max(self.getHeight(root.left),self.getHeight(root.right))
T=int(raw_input())
myTree=Solution()
root=None
for i in range(T):
data=int(raw_input())
root=myTree.insert(root,data)
height=myTree.getHeight(root)
print height