🧠5 LeetCode Problems Explained with Code + Video
Part of the DSA Decoded series — step-by-step Python solutions with video walkthroughs.
Problems covered in this post:
- 🟢 Diameter of Binary Tree (LeetCode #543) · Easy · Trees
- 🟢 Same Tree (LeetCode #100) · Easy · Trees
- 🟡 Binary Tree Level Order Traversal (LeetCode #102) · Medium · Trees
- 🟡 Number of Islands (LeetCode #200) · Medium · Graphs
- 🟡 Clone Graph (LeetCode #133) · Medium · Graphs
🟢 Diameter of Binary Tree
Difficulty: Easy  | Topic: Trees  | LeetCode #543
Problem
Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Solution
def diameter_of_binary_tree(root):
result = [0]
def depth(node):
if not node:
return 0
left = depth(node.left)
right = depth(node.right)
result[0] = max(result[0], left + right)
return 1 + max(left, right)
depth(root)
return result[0]
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟢 Same Tree
Difficulty: Easy  | Topic: Trees  | LeetCode #100
Problem
Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Solution
def is_same_tree(p, q):
if not p and not q:
return True
if not p or not q or p.val != q.val:
return False
return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟡 Binary Tree Level Order Traversal
Difficulty: Medium  | Topic: Trees  | LeetCode #102
Problem
Given the root of a binary tree, return the level order traversal of its nodes' values (i.e., from left to right, level by level).
Solution
from collections import deque
def level_order(root):
if not root:
return []
result, queue = [], deque([root])
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
result.append(level)
return result
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟡 Number of Islands
Difficulty: Medium  | Topic: Graphs  | LeetCode #200
Problem
Given an m x n 2D binary grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
Solution
def num_islands(grid):
if not grid:
return 0
count = 0
def dfs(r, c):
if r < 0 or r >= len(grid) or c < 0 or c >= len(grid[0]) or grid[r][c] != '1':
return
grid[r][c] = '0'
dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1)
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == '1':
dfs(r, c)
count += 1
return count
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟡 Clone Graph
Difficulty: Medium  | Topic: Graphs  | LeetCode #133
Problem
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list of its neighbors.
Solution
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = neighbors or []
def clone_graph(node):
if not node:
return None
clones = {}
def dfs(n):
if n in clones:
return clones[n]
clone = Node(n.val)
clones[n] = clone
for nb in n.neighbors:
clone.neighbors.append(dfs(nb))
return clone
return dfs(node)
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🔔 Follow Along
New problems every 2 days — subscribe to stay consistent with your DSA prep!
Drop a comment with your approach — always curious to see other solutions!
Top comments (0)