Skip to content

Commit 8a89c72

Browse files
committed
2 parents fd40d21 + de2c12e commit 8a89c72

File tree

90 files changed

+4337
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+4337
-7
lines changed

.github/ISSUE_TEMPLATE/---issue--feature.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ assignees: ''
88
---
99

1010
**Is your feature request related to a problem? Please describe.**
11-
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
11+
A clear and concise description of what the problem is.
1212

1313
**Describe the solution you'd like**
1414
A clear and concise description of what you want to happen.
1515

16-
**Describe alternatives you've considered**
17-
A clear and concise description of any alternative solutions or features you've considered.
18-
1916
**Additional context**
2017
Add any other context or screenshots about the feature request here.

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### VisualStudioCode ###
2+
.vscode/*
3+
!.vscode/settings.json
4+
!.vscode/tasks.json
5+
!.vscode/launch.json
6+
!.vscode/extensions.json
7+
*.code-workspace
8+
9+
# Local History for Visual Studio Code
10+
.history/
11+
12+
### VisualStudioCode Patch ###
13+
# Ignore all local history of files
14+
.history
15+
.ionide

DSA 450 GFG/BTtoBST.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Link : https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1#
2+
3+
'''
4+
# Tree Node
5+
class Node:
6+
def __init__(self, val):
7+
self.right = None
8+
self.data = val
9+
self.left = None
10+
'''
11+
class Solution:
12+
13+
# The given root is the root of the Binary Tree
14+
# Return the root of the generated BST
15+
16+
def BTtoArray(self , root , array):
17+
18+
if root is None:
19+
return
20+
21+
self.BTtoArray(root.left , array)
22+
23+
array.append(root.data)
24+
25+
self.BTtoArray(root.right , array)
26+
27+
28+
def countNodes(self,root):
29+
30+
if root is None:
31+
return 0
32+
33+
return self.countNodes(root.left) + self.countNodes(root.right) + 1
34+
35+
def ArraytoBST(self,array,root):
36+
37+
if root is None:
38+
return
39+
40+
self.ArraytoBST(array , root.left)
41+
42+
root.data = array[0]
43+
array.pop(0)
44+
45+
self.ArraytoBST(array , root.right)
46+
47+
48+
def binaryTreeToBST(self, root):
49+
# code here
50+
51+
if root is None:
52+
return
53+
54+
# Find the number of nodes to create an array of that size
55+
n = self.countNodes(root)
56+
57+
arr = []*n
58+
59+
# Store the inorder traversal of tree in an array
60+
self.BTtoArray(root , arr)
61+
62+
# Sort the array
63+
arr.sort()
64+
65+
# Tranfer the elements from root to the tree and return the BST
66+
self.ArraytoBST(arr , root)
67+
68+
69+
70+
71+
#{
72+
# Driver Code Starts
73+
#Initial Template for Python 3
74+
75+
from collections import deque
76+
# Tree Node
77+
class Node:
78+
def __init__(self, val):
79+
self.right = None
80+
self.data = val
81+
self.left = None
82+
83+
# Function to Build Tree
84+
def buildTree(s):
85+
#Corner Case
86+
if(len(s)==0 or s[0]=="N"):
87+
return None
88+
89+
# Creating list of strings from input
90+
# string after spliting by space
91+
ip=list(map(str,s.split()))
92+
93+
# Create the root of the tree
94+
root=Node(int(ip[0]))
95+
size=0
96+
q=deque()
97+
98+
# Push the root to the queue
99+
q.append(root)
100+
size=size+1
101+
102+
# Starting from the second element
103+
i=1
104+
while(size>0 and i<len(ip)):
105+
# Get and remove the front of the queue
106+
currNode=q[0]
107+
q.popleft()
108+
size=size-1
109+
110+
# Get the current node's value from the string
111+
currVal=ip[i]
112+
113+
# If the left child is not null
114+
if(currVal!="N"):
115+
116+
# Create the left child for the current node
117+
currNode.left=Node(int(currVal))
118+
119+
# Push it to the queue
120+
q.append(currNode.left)
121+
size=size+1
122+
# For the right child
123+
i=i+1
124+
if(i>=len(ip)):
125+
break
126+
currVal=ip[i]
127+
128+
# If the right child is not null
129+
if(currVal!="N"):
130+
131+
# Create the right child for the current node
132+
currNode.right=Node(int(currVal))
133+
134+
# Push it to the queue
135+
q.append(currNode.right)
136+
size=size+1
137+
i=i+1
138+
return root
139+
140+
def printInorder(root):
141+
if root is None:
142+
return
143+
printInorder(root.left)
144+
print (root.data, end=' ')
145+
printInorder(root.right)
146+
147+
if __name__=="__main__":
148+
t=int(input())
149+
for _ in range(0,t):
150+
s=input()
151+
root=buildTree(s)
152+
Solution().binaryTreeToBST(root)
153+
printInorder(root)
154+
print()
155+
# } Driver Code Ends

DSA 450 GFG/BuyandSell.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Link to the problem : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
3+
class Solution:
4+
def maxProfit(self, prices):
5+
min_so_far = prices[0]
6+
max_profit = 0
7+
n = len(prices)
8+
for i in prices :
9+
min_so_far = min(min_so_far , i)
10+
profit = i - min_so_far
11+
max_profit = max(max_profit , profit)
12+
return max_profit

DSA 450 GFG/CountAndSay.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Problem : https://leetcode.com/problems/count-and-say/
2+
3+
# Input: n = 4
4+
# Output: "1211"
5+
# Explanation:
6+
# countAndSay(1) = "1"
7+
# countAndSay(2) = say "1" = one 1 = "11"
8+
# countAndSay(3) = say "11" = two 1's = "21"
9+
# countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
10+
11+
# Initialize counter = 1 to store the count of every element
12+
# Initialize o/p string as ret = ""
13+
# If the previous element in the string is equal to the current element in the string , increament the counter
14+
# else Concatenate the count and the previous element of the string in ret
15+
16+
# Return ret
17+
18+
class Solution:
19+
def countAndSay(self, n):
20+
if (n == 1):
21+
return ("1")
22+
23+
s = self.countAndSay(n-1)
24+
25+
ret = ""
26+
cnt = 1
27+
i = 1
28+
while i < len(s) + 1:
29+
if i < len(s) and s[i] == s[i-1]:
30+
cnt += 1
31+
else:
32+
ret += str(cnt) + str(s[i-1])
33+
cnt = 1
34+
i += 1
35+
36+
return (ret)

DSA 450 GFG/DeleteNodeinBST.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#https://leetcode.com/problems/delete-node-in-a-bst/submissions/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
11+
def findMinimum(self , root):
12+
current = root
13+
while(current.left):
14+
current = current.left
15+
return current
16+
17+
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
18+
19+
if not root:
20+
return root
21+
22+
23+
elif(key < root.val):
24+
root.left = self.deleteNode(root.left , key)
25+
26+
elif(key > root.val):
27+
root.right = self.deleteNode(root.right , key)
28+
29+
else:
30+
31+
#Leaf
32+
if not root.left and not root.right:
33+
root = None
34+
35+
#1 child
36+
elif not root.left:
37+
root = root.right
38+
39+
elif not root.right :
40+
root = root.left
41+
42+
#2 child
43+
else:
44+
temp = self.findMinimum(root.right)
45+
root.val = temp.val
46+
root.right = self.deleteNode(root.right , temp.val)
47+
48+
return root
49+
50+

0 commit comments

Comments
 (0)