Skip to content

Instantly share code, notes, and snippets.

View iamprayush's full-sized avatar

Prayush Dawda iamprayush

  • Engineer at cred | GSoC '20 @oppia
  • India
View GitHub Profile
@iamprayush
iamprayush / main.py
Last active September 20, 2020 12:37
Binary Search Tree Iterator
class BSTIterator:
def __init__(self, root: TreeNode):
self.stack = []
self.curr_node = root
def next(self) -> int:
while self.curr_node:
self.stack.append(self.curr_node)
self.curr_node = self.curr_node.left
self.curr_node = self.stack.pop()
@iamprayush
iamprayush / main.py
Created September 19, 2020 14:16
Find a pair with a given sum in BST
class Solution:
def findTarget(self, root: TreeNode, k: int) -> bool:
arr, stack = [], []
curr_node = root
while curr_node or stack:
if curr_node:
stack.append(curr_node)
curr_node = curr_node.left
else:
@iamprayush
iamprayush / main.py
Created September 19, 2020 14:11
Kth Smallest Element in a BST
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
stack = []
curr = root
while curr or stack:
if curr:
stack.append(curr)
curr = curr.left
else:
@iamprayush
iamprayush / main.py
Created September 18, 2020 10:14
Lowest Common Ancestor of a Binary Search Tree
class Solution:
def lowestCommonAncestor(self, node, p, q):
if not node:
return None
if p.val < node.val < q.val or p.val > node.val > q.val or node.val in (p.val, q.val):
return node
if p.val < node.val and q.val < node.val:
# Both nodes are on current node's left.
return self.lowestCommonAncestor(node.left, p, q)
# Otherwise both nodes are on current node's right.
@iamprayush
iamprayush / main.py
Created September 18, 2020 10:09
Validate Binary Search Tree
class Solution:
def isValidBST(self, node, mini=-inf, maxi=inf):
if not node:
return True
if not mini < node.val < maxi:
return False
left_is_valid, right_is_valid = True, True
if node.left:
if node.left.val >= node.val:
left_is_valid = False
@iamprayush
iamprayush / main.py
Created September 18, 2020 09:49
Construct Binary Search Tree from Preorder Traversal
# 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 bstFromPreorder(self, preorder: List[int]) -> TreeNode:
root = TreeNode(preorder[0])
@iamprayush
iamprayush / main.py
Created September 18, 2020 09:39
Search in a Binary Search Tree
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return None
if root.val == val:
return root
if root.val > val:
# Current val is too high so move left so we get lower vals.
return self.searchBST(root.left, val)
# Otherwise move right so we get higher vals.
@iamprayush
iamprayush / main.py
Created September 18, 2020 09:36
Populating Next Right Pointers in Each Node
class Solution:
def connect(self, node):
if not node:
return None
# For every node...
# 1. We will connect it's left child to it's right child. (If it has children).
if node.left and node.right:
node.left.next = node.right
# 2. Connect it's right child to the current node's next's left child. (If it has a next node).
if node.next:
@iamprayush
iamprayush / main.py
Created September 17, 2020 14:07
Flatten Binary Tree to Linked List
class Solution:
def flatten(self, root: TreeNode) -> None:
# If current node is null, or it's a leaf, we don't do shit.
if root is None or (root.left is None and root.right is None):
return
if root.left:
self.flatten(root.left)
original_right = root.right
root.right = root.left
root.left = None
@iamprayush
iamprayush / main.py
Created September 17, 2020 12:13
Symmetric Tree
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
return self.isMirror(root, root)
def isMirror(self, left_node: TreeNode, right_node: TreeNode) -> bool:
if not left_node and not right_node:
return True
if (not left_node) or (not right_node):
return False
if left_node.val != right_node.val: