Skip to content

Commit

Permalink
some change
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaansarii committed Aug 15, 2024
1 parent f3191dd commit 0e581c6
Show file tree
Hide file tree
Showing 6,381 changed files with 944,523 additions and 119 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
49 changes: 46 additions & 3 deletions BST/Easy/06_Search_and_Insertion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
# Problem URL: https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/
# TODO: Implement the solution

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

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

def searchNode(self,root,key):
if root:
if root.val==key: return True
if root.val>key: return self.searchNode(root.left,key)
else: return self.searchNode(root.right,key)
return -1


def LevelOrder(self,root):
if not root:
return
result=[]
Queue=[root]
while Queue:
curr=Queue.pop(0)
result.append(curr.val)
if curr.left:
Queue.append(curr.left)
if curr.right:
Queue.append(curr.right)
return result

if __name__ == "__main__":
root = Node(40)
root.insert(root, 23)
root.insert(root, 43)
root.insert(root, 33)
root.insert(root, 53)
root.insert(root, 63)

print("Level Order Traversal:", root.LevelOrder(root))
print("Search for 23:", root.searchNode(root, 23))
print("Search for 100:", root.searchNode(root, 100))
11 changes: 10 additions & 1 deletion BST/Medium/236. Lowest Common Ancestor of a Binary Tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
# self.left = None
# self.right = None

# 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':

# if root in (None,p,q): return root
# left,right=self.lowestCommonAncestor(root.left,p,q),self.lowestCommonAncestor(root.right,p,q)
# return root if left and right else right or left
23 changes: 22 additions & 1 deletion BST/Medium/99. Recover Binary Search Tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#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
# Definition for a binary tree node.

# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
Expand All @@ -11,4 +18,18 @@ def recoverTree(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""

def inorder(root):
if not root:return []
return inorder(root.left) + [root] + inorder(root.right)

nodes=inorder(root)
swap1,swap2=None,None
for i in range(len(nodes)-1):
if nodes[i].val>nodes[i+1].val:
swap2=nodes[i+1]
if not swap1:
swap1=nodes[i]
else:
break

swap1.val,swap2.val=swap2.val,swap1.val
Loading

0 comments on commit 0e581c6

Please sign in to comment.