Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e378bd

Browse files
Height of Binary Search Tree
1 parent e7ac1f0 commit 7e378bd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎Day 22: Binary Search Trees.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Node:
2+
def __init__(self,data):
3+
self.right=self.left=None
4+
self.data = data
5+
class Solution:
6+
def insert(self,root,data):
7+
if root==None:
8+
return Node(data)
9+
else:
10+
if data<=root.data:
11+
cur=self.insert(root.left,data)
12+
root.left=cur
13+
else:
14+
cur=self.insert(root.right,data)
15+
root.right=cur
16+
return root
17+
18+
def getHeight(self,root):
19+
depth = 0
20+
q = []
21+
q.append(root)
22+
q.append(None)
23+
while(len(q) > 0):
24+
temp = q[0]
25+
q = q[1:]
26+
if(temp == None):
27+
depth += 1
28+
29+
if(temp != None):
30+
if(temp.left):
31+
q.append(temp.left)
32+
33+
if(temp.right):
34+
q.append(temp.right)
35+
elif(len(q) > 0):
36+
q.append(None)
37+
return depth-1
38+
39+
T=int(input())
40+
myTree=Solution()
41+
root=None
42+
for i in range(T):
43+
data=int(input())
44+
root=myTree.insert(root,data)
45+
height=myTree.getHeight(root)
46+
print(height)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /