Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaansarii committed Aug 3, 2024
1 parent df3b108 commit 28fd8ce
Show file tree
Hide file tree
Showing 49 changed files with 264 additions and 9 deletions.
21 changes: 21 additions & 0 deletions BST/36_Minimum_Element_In_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Problem: Minimum_Element_In_BST
# URL: https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1

#User function Template for python3

"""
class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
"""

class Solution:
#Function to find the minimum element in the given BST.
def minValue(self, root):
##Your code here
if (root.left==None):
return root.data
return self.minValue(root.left)

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions BST/Easy/01_Ceil_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/ceil-in-a-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 01_Ceil_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Easy/02_Floor_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/floor-in-a-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 02_Floor_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Easy/03_Insert_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/insert-into-a-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 03_Insert_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Easy/04_Introduction_to_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/introduction-to-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 04_Introduction_to_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Easy/05_Search_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/search-in-a-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 05_Search_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Easy/06_Search_and_Insertion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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
6 changes: 6 additions & 0 deletions BST/Hard/01_Merge_2_BSTs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/merge-two-binary-search-trees
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 01_Merge_2_BSTs.py
pass
6 changes: 6 additions & 0 deletions BST/Hard/02_Recover_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/recover-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 02_Recover_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Hard/03_Largest_BST_in_Binary_Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://www.geeksforgeeks.org/largest-bst/
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 03_Largest_BST_in_Binary_Tree.py
pass
6 changes: 6 additions & 0 deletions BST/Hard/04_Convert_Normal_BST_to_Balanced_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://www.geeksforgeeks.org/convert-normal-bst-balanced-bst/
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 04_Convert_Normal_BST_to_Balanced_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Hard/05_Flatten_BST_to_Sorted_List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://www.geeksforgeeks.org/flatten-bst-to-sorted-list-increasing-order/
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 05_Flatten_BST_to_Sorted_List.py
pass
6 changes: 6 additions & 0 deletions BST/Hard/06_Merge_Two_Balanced_BSTs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://www.geeksforgeeks.org/merge-two-balanced-binary-search-trees/
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 06_Merge_Two_Balanced_BSTs.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/01_Delete_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/delete-node-in-a-binary-search-tree
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 01_Delete_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/02_Find_Kth_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/kth-smallest-element-in-a-bst
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 02_Find_Kth_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/03_Check_BST_or_BT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/check-for-bst/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 03_Check_BST_or_BT.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/04_LCA_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/lowest-common-ancestor-in-a-bst/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 04_LCA_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/05_Construct_BST_from_Preorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 05_Construct_BST_from_Preorder.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/06_Inorder_Successor_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/predecessor-and-successor/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 06_Inorder_Successor_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/07_Two_Sum_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://leetcode.com/problems/two-sum-iv-input-is-a-bst
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 07_Two_Sum_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/08_Find_Min_Max_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 08_Find_Min_Max_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/09_Binary_Tree_to_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 09_Binary_Tree_to_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/10_Count_BST_Nodes_in_Range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/count-bst-nodes-that-lie-in-a-given-range/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 10_Count_BST_Nodes_in_Range.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/11_Brothers_from_Different_Root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/brothers-from-different-root/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 11_Brothers_from_Different_Root.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/12_Find_Median_in_BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://www.geeksforgeeks.org/find-median-bst-time-o1-space/
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 12_Find_Median_in_BST.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/13_Check_Whether_BST_Contains_Dead_End.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/check-whether-bst-contains-dead-end/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 13_Check_Whether_BST_Contains_Dead_End.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/14_Preorder_to_Postorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/preorder-to-postorder/0
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 14_Preorder_to_Postorder.py
pass
6 changes: 6 additions & 0 deletions BST/Medium/15_Populate_Inorder_Successor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Problem URL: https://practice.geeksforgeeks.org/problems/populate-inorder-successor-for-all-nodes/1
# TODO: Implement the solution

def solution():
# TODO: Implement the solution for 15_Populate_Inorder_Successor.py
pass
60 changes: 60 additions & 0 deletions BST/file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

# List of problems with category, filename, and URL
problems=(
# Easy
"Easy 01_Ceil_in_BST.py https://leetcode.com/problems/ceil-in-a-binary-search-tree"
"Easy 02_Floor_in_BST.py https://leetcode.com/problems/floor-in-a-binary-search-tree"
"Easy 03_Insert_in_BST.py https://leetcode.com/problems/insert-into-a-binary-search-tree"
"Easy 04_Introduction_to_BST.py https://leetcode.com/problems/introduction-to-binary-search-tree"
"Easy 05_Search_in_BST.py https://leetcode.com/problems/search-in-a-binary-search-tree"
"Easy 06_Search_and_Insertion.py https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/"

# Medium
"Medium 01_Delete_in_BST.py https://leetcode.com/problems/delete-node-in-a-binary-search-tree"
"Medium 02_Find_Kth_in_BST.py https://leetcode.com/problems/kth-smallest-element-in-a-bst"
"Medium 03_Check_BST_or_BT.py https://practice.geeksforgeeks.org/problems/check-for-bst/1"
"Medium 04_LCA_in_BST.py https://practice.geeksforgeeks.org/problems/lowest-common-ancestor-in-a-bst/1"
"Medium 05_Construct_BST_from_Preorder.py https://www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/"
"Medium 06_Inorder_Successor_in_BST.py https://practice.geeksforgeeks.org/problems/predecessor-and-successor/1"
"Medium 07_Two_Sum_in_BST.py https://leetcode.com/problems/two-sum-iv-input-is-a-bst"
"Medium 08_Find_Min_Max_in_BST.py https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1"
"Medium 09_Binary_Tree_to_BST.py https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1"
"Medium 10_Count_BST_Nodes_in_Range.py https://practice.geeksforgeeks.org/problems/count-bst-nodes-that-lie-in-a-given-range/1"
"Medium 11_Brothers_from_Different_Root.py https://practice.geeksforgeeks.org/problems/brothers-from-different-root/1"
"Medium 12_Find_Median_in_BST.py https://www.geeksforgeeks.org/find-median-bst-time-o1-space/"
"Medium 13_Check_Whether_BST_Contains_Dead_End.py https://practice.geeksforgeeks.org/problems/check-whether-bst-contains-dead-end/1"
"Medium 14_Preorder_to_Postorder.py https://practice.geeksforgeeks.org/problems/preorder-to-postorder/0"
"Medium 15_Populate_Inorder_Successor.py https://practice.geeksforgeeks.org/problems/populate-inorder-successor-for-all-nodes/1"

# Hard
"Hard 01_Merge_2_BSTs.py https://leetcode.com/problems/merge-two-binary-search-trees"
"Hard 02_Recover_BST.py https://leetcode.com/problems/recover-binary-search-tree"
"Hard 03_Largest_BST_in_Binary_Tree.py https://www.geeksforgeeks.org/largest-bst/"
"Hard 04_Convert_Normal_BST_to_Balanced_BST.py https://www.geeksforgeeks.org/convert-normal-bst-balanced-bst/"
"Hard 05_Flatten_BST_to_Sorted_List.py https://www.geeksforgeeks.org/flatten-bst-to-sorted-list-increasing-order/"
"Hard 06_Merge_Two_Balanced_BSTs.py https://www.geeksforgeeks.org/merge-two-balanced-binary-search-trees/"
)

# Create directories
mkdir -p Easy Medium Hard

# Create .py files
for problem in "${problems[@]}"; do
IFS=' ' read -r category filename url <<< "$problem"
filepath="$category/$filename"

# Check if the file already exists
if [[ ! -f "$filepath" ]]; then
echo "# Problem URL: $url" > "$filepath"
echo "# TODO: Implement the solution" >> "$filepath"
echo >> "$filepath"
echo "def solution():" >> "$filepath"
echo " # TODO: Implement the solution for $filename" >> "$filepath"
echo " pass" >> "$filepath"

echo "Created $filepath"
else
echo "$filepath already exists, skipping."
fi
done
6 changes: 0 additions & 6 deletions Tree/BST/36_Minimum_Element_In_BST.py

This file was deleted.

24 changes: 21 additions & 3 deletions Tree/Hard/87_Validate_Binary_Search_Tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# Problem: Validate_Binary_Search_Tree
# URL: https://leetcode.com/problems/validate-binary-search-tree/description/

def solution():
# TODO: Implement the solution for Validate_Binary_Search_Tree
pass
# 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 isValidBST(self, root: Optional[TreeNode]) -> bool:
def is_valid(node, lower=float('-inf'), upper=float('inf')):
if not node:
return True
val = node.val
if val <= lower or val >= upper:
return False
if not is_valid(node.right, val, upper):
return False
if not is_valid(node.left, lower, val):
return False
return True

return is_valid(root)

0 comments on commit 28fd8ce

Please sign in to comment.