Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaansarii committed Aug 10, 2024
1 parent 7c5c95e commit 660e047
Show file tree
Hide file tree
Showing 152 changed files with 3,917 additions and 4,498 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions BST/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 1 addition & 1 deletion BST/Easy/01_Ceil_in_BST.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Problem URL: https://leetcode.com/problems/ceil-in-a-binary-search-tree
# Problem URL: https://www.geeksforgeeks.org/problems/implementing-ceil-in-bst/1
# TODO: Implement the solution

def solution():
Expand Down
12 changes: 12 additions & 0 deletions BST/Medium/236. Lowest Common Ancestor of a Binary Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':

77 changes: 74 additions & 3 deletions BST/Medium/43_Delete_Node_In_A_BST.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,77 @@
# Problem: Delete_Node_In_A_BST
# URL: https://leetcode.com/problems/delete-node-in-a-bst/

def solution():
# TODO: Implement the solution for Delete_Node_In_A_BST
pass
class Node:
def __init__(self,data) -> None:
self.val=data
self.left=None
self.right=None


class BST:
def insert(self,root,key):
if not root:
return Node(key)
if root.val>key:
root.left=self.insert(root.left,key)
elif root.val<key:
root.right=self.insert(root.right,key)
return root


def LevelOrder(self,root):
if not root:
return root
queue=[root]
while queue:
current=queue.pop(0)
print(current.val, end=' ')
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)

def minNode(self,root):
current=root
while current and current.left:
current=current.left
return current

def maxNode(self,root):
current=root
while current and current.right:
current=current.right
return current

def deleteNode(self,root,key):
if not root:
return root

if root.val>key:
root.left=self.deleteNode(root.left,key)
elif root.val<key:
root.right=self.deleteNode(root.right,key)
else:
if not root.left:
return root.right
elif not root.right:
return root.left
tempNode=self.minNode(root.right)
root.val=tempNode.val
root.right=self.deleteNode(root.right,tempNode.val)
return root

if __name__=="__main__":
root=Node(5)
bst=BST()
arr=[3,6,2,4,7]
for i in arr:
bst.insert(root,i)
bst.LevelOrder(root)
print()
bst.deleteNode(root,3)
bst.LevelOrder(root)
print()



14 changes: 14 additions & 0 deletions BST/Medium/99. Recover Binary Search Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#https://leetcode.com/problems/recover-binary-search-tree/description/

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def recoverTree(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""

45 changes: 45 additions & 0 deletions BST/Traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Node:
def __init__(self,data):
self.left=None
self.right=None
self.data=data

def insert(root,data):
if root is None:
return Node(data)
if root.data==data:
return root
if root.data<data:
root.right=insert(root.right,data)
if root.data>data:
root.left=insert(root.left,data)
return root

def InOrder(root):
if root:
InOrder(root.left)
print(root.data, end=' ')
InOrder(root.right)
def preOrder(root):
if root:
print(root.data, end=' ')
preOrder(root.left)
preOrder(root.right)
def postOrder(root):
if root:
postOrder(root.left)
postOrder(root.right)
print(root.data, end=' ')

if __name__ == '__main__':
root=Node(100)
insert(root,34)
insert(root,54)
insert(root,74)
insert(root,38)
insert(root,30)
InOrder(root)
print()
preOrder(root)
print()
postOrder(root)
Loading

0 comments on commit 660e047

Please sign in to comment.