2026 June 27 Problems

56 views
Skip to first unread message

daryl...@gmail.com

unread,
Jun 27, 2026, 12:39:54 PM (12 days ago) Jun 27
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.


617. Merge Two Binary Trees Easy 79.1%

814. Binary Tree Pruning Medium Medium 72.5%

827. Making A Large Island Hard 56.8%

Here is the meeting url:

Full details:

Topic: Leet Code meeting
Time: Mar 28, 2026 10:00 AM Pacific Time (US and Canada)
        Every week on Sat, 110 occurrence(s)
Please download and import the following iCalendar (.ics) files to your calendar system.
Weekly: https://us05web.zoom.us/meeting/tZYocuqgqzMuH9IuHUphHIiZ_4T4IndJLToX/ics?icsToken=DJFc88pL6-me_TneLwAALAAAAA70ShjYgLH6RfEnqUm5LK03v2OgtbXujwlKXRMVgIVWCY9aONPJMCycMlkhyPZ3XSrMVVgXDSYlGF_r7DAwMDAwMQ&meetingMasterEventId=MGJmt5lJTd6mPygcS0hiAQ
Join Zoom Meeting
https://us05web.zoom.us/j/82553858456?pwd=fnK5Vb0CcfFpv0lnouILjeEs2z2Khc.1

Meeting ID: 825 5385 8456
Passcode: 182596

Anuj Patnaik

unread,
Jun 27, 2026, 1:40:18 PM (12 days ago) Jun 27
to leetcode-meetup
# 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 mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 == None:
return root2
if root2 == None:
return root1
root1.val = root1.val + root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
return root1

Anuj Patnaik

unread,
Jun 27, 2026, 1:40:35 PM (12 days ago) Jun 27
to leetcode-meetup
# 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 pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root == None:
return None
root.left = self.pruneTree(root.left)
root.right = self.pruneTree(root.right)
if root.val == 0 and root.left is None and root.right is None:
return None

return root

Jagrut

unread,
Jun 27, 2026, 2:10:45 PM (12 days ago) Jun 27
to leetcode-meetup
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return None
new_node = TreeNode((root1.val if root1 else 0) + (root2.val if root2 else 0))
new_node.left = self.mergeTrees(
root1.left if root1 else None,
root2.left if root2 else None)
new_node.right = self.mergeTrees(
root1.right if root1 else None,
root2.right if root2 else None)
return new_node

Jagrut

unread,
Jun 27, 2026, 4:16:18 PM (12 days ago) Jun 27
to leetcode-meetup
class Solution:
# True = 1 found, False = 1 not found
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
return root if self._prune(root) else (None if root.val == 0 else root)

def _prune(self, node):
if node:
left_result = self._prune(node.left)
right_result = self._prune(node.right)

# What to do with the left and right subtrees?
if not left_result:
node.left = None
if not right_result:
node.right = None
# What the parent should do with me
return True if (left_result or right_result or node.val == 1) else False
# leaf node - 1 is not present
return False

Jagrut

unread,
Jun 27, 2026, 6:19:24 PM (12 days ago) Jun 27
to leetcode-meetup
class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
# Common items
N = len(grid)
# island-label -> island-size mapping
island_sizes = defaultdict(int)
next_land_label = 2
# maximum connection possibly by flipping one cell
# 1 can be case when all cells are 0
res = 1
neighbor_incs = [[0, 1], [0, -1], [1, 0], [-1, 0]]

"""
Stage 1:
Scan grid. Identify and uniquely label each island.
At the same time, also find and save the size of the island, retrievable by its label (in hashmap/dictionary)
Also, keep storing the maximum size island you have seen so far. (in variable)
"""
for r in range(N):
for c in range(N):
if grid[r][c] == 1:
island_sizes[next_land_label] = self._dfs(r, c, N, next_land_label, grid, neighbor_incs)
res = max(res, island_sizes[next_land_label])
next_land_label += 1

"""
Stage 2:
Flip each possible water cell to land.
For each water cell, count how many islands it can connect to via its 4 neighbors.
Keep updating the result.
"""
for r in range(N):
for c in range(N):
if grid[r][c] == 0:
connected_size_here = self._make_connection(r, c, N, grid, island_sizes, neighbor_incs)
res = max(res, connected_size_here)

return res

"""
Stage 1 work
DFS to find size of island, label island
"""
def _dfs(self, r, c, N, label, grid, neighbor_incs):
island_size = 0
if self.is_within_bounds(r, c, N) and grid[r][c] == 1:
grid[r][c] = label
island_size = 1
for r_inc, c_inc in neighbor_incs:
island_size += self._dfs(r + r_inc, c + c_inc, N, label, grid, neighbor_incs)
return island_size

"""
Stage 2 work
Make connection among the 4 possible islands.
"""
def _make_connection(self, r, c, N, grid, island_sizes, neighbor_incs):
seen_land_labels = set()
connected_size_here = 1
for r_inc, c_inc in neighbor_incs:
r_new = r + r_inc
c_new = c + c_inc
if (
self.is_within_bounds(r_new, c_new, N)
and grid[r_new][c_new] > 1 and grid[r_new][c_new] not in seen_land_labels
):
connected_size_here += island_sizes[grid[r_new][c_new]]
seen_land_labels.add(grid[r_new][c_new])
return connected_size_here

"""
Coordinate check to be within grid
"""
def is_within_bounds(self, r, c, N):
return r >= 0 and r < N and c >= 0 and c < N

Reply all
Reply to author
Forward
0 new messages