forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.py
51 lines (47 loc) · 1.44 KB
/
sol.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
45
46
47
48
49
50
51
class TreeNode:
def __init__(self, val = None):
self.val = val
self.left = None
self.right = None
def insertNode(self, val):
if self.val is None:
self.val = val
elif val<=self.val:
if self.left is None:
self.left = TreeNode(val)
else:
self.left.insertNode(val)
else:
if self.right is None:
self.right = TreeNode(val)
else:
self.right.insertNode(val)
def preOrderTraversal(self):
A = []
if self is not None:
A.append(self.val)
if self.left is not None:
A.extend(self.left.preOrderTraversal())
if self.right is not None:
A.extend(self.right.preOrderTraversal())
return A
def secondHighestElement(self):
A = self.preOrderTraversal()
if len(A)<=2:
return min(A)
h1, h2 = A[0], A[1]
for i in range(2, len(A)):
if A[i]>h1 and A[i]>h2:
h1, h2 = A[i], h1
elif A[i]>h2:
h2 = A[i]
else:
pass
return h2
if __name__=='__main__':
import random
myTree = TreeNode()
for _ in range(10):
val = random.randint(1, 100)
myTree.insertNode(val)
print('Keys =', myTree.preOrderTraversal(), '2nd Highest Element =', myTree.secondHighestElement())