Skip to content

added leetcode questions #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions 14. Questions/leetcode 1971 - find if path exists in a graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# find if path exists in a graph | leetcode 1971 | https://leetcode.com/problems/find-if-path-exists-in-graph/
# method: adjacency list, visited and toVisit lists

from collections import defaultdict

class Solution:
def validPath(self, n: int, edges: list[list[int]], source: int, destination: int) -> bool:
# edge case
if n == 1 and source == destination:
return True

edgeMap = defaultdict(list) # adjacency list
for edge in edges:
edgeMap[edge[0]].append(edge[1])
edgeMap[edge[1]].append(edge[0])

visited = set() # set of visited nodes
toVisit = [edgeMap[source]] # set of nodes to visit

# while there are nodes to visit
while toVisit:

# this node is now visited
nodes = toVisit.pop()

# for each node in the adjacent nodes
for node in nodes:
if node == destination:
return True

# if node wasn't visited
# visit its adjacent nodes
elif node not in visited:
visited.add(node)
toVisit.append(edgeMap[node])

# if node was visited
# do nothing

# if no more nodes to visit
# and still no path
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# count nodes equal to average of subtree | leetcode 2265 | https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/
# method: dfs, update size and sum of subtree at each node and check for average

# 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 averageOfSubtree(self, root: list[TreeNode]) -> int:
self.counter = 0
def dfs(node):
if node is None:
return 0, 0

lSize, lSum = dfs(node.left)
rSize, rSum = dfs(node.right)

nSize, nSum = lSize + rSize + 1, lSum + rSum + node.val
if (nSum // nSize) == node.val:
self.counter += 1

return nSize, nSum

dfs(root)
return self.counter

22 changes: 22 additions & 0 deletions 14. Questions/leetcode 2331 - evaluate boolean binary tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# evaluate boolean binary tree | leetcode 2331 | https://leetcode.com/problems/evaluate-boolean-binary-tree/
# method: dfs, evaluate left and/or right, return node's value

# 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 evaluateTree(self, node):
if node.left is None and node.right is None:
return node.val

if node.val == 2:
node.val = bool(self.evaluateTree(node.left)) or bool(self.evaluateTree(node.right))

if node.val == 3:
node.val = bool(self.evaluateTree(node.left)) and bool(self.evaluateTree(node.right))

return node.val
23 changes: 23 additions & 0 deletions 14. Questions/leetcode 235 - lowest common ancestor in bst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# lowest common ancestor in binary search tree | leetcode 235 | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
# method: iteration through each node, when p and q are in different subtrees, current node is LCA

# 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':
cur = root

while cur:
if p.val > cur.val and q.val > cur.val:
cur = cur.right
elif p.val < cur.val and q.val < cur.val:
cur = cur.left
else:
return cur

return root