-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c5c95e
commit 660e047
Showing
152 changed files
with
3,917 additions
and
4,498 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
BST/Medium/236. Lowest Common Ancestor of a Binary Tree.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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': | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.