diff --git a/14. Questions/leetcode 1971 - find if path exists in a graph.py b/14. Questions/leetcode 1971 - find if path exists in a graph.py new file mode 100644 index 0000000..317fb94 --- /dev/null +++ b/14. Questions/leetcode 1971 - find if path exists in a graph.py @@ -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 diff --git a/14. Questions/leetcode 2265 - count nodes equal to average of subtree.py b/14. Questions/leetcode 2265 - count nodes equal to average of subtree.py new file mode 100644 index 0000000..f5d5004 --- /dev/null +++ b/14. Questions/leetcode 2265 - count nodes equal to average of subtree.py @@ -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 + diff --git a/14. Questions/leetcode 2331 - evaluate boolean binary tree.py b/14. Questions/leetcode 2331 - evaluate boolean binary tree.py new file mode 100644 index 0000000..9312bc8 --- /dev/null +++ b/14. Questions/leetcode 2331 - evaluate boolean binary tree.py @@ -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 diff --git a/14. Questions/leetcode 235 - lowest common ancestor in bst.py b/14. Questions/leetcode 235 - lowest common ancestor in bst.py new file mode 100644 index 0000000..121e420 --- /dev/null +++ b/14. Questions/leetcode 235 - lowest common ancestor in bst.py @@ -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