π§ Day 7: Exploring Trees, Strings & Revisiting Linked Lists!


Welcome back, tech enthusiasts! On Day 7 of the 90 Days DSA Challenge, I explored a mix of Binary Tree Traversals and a String pattern search problem, and revised two previously covered Linked List problems. This blend of problems gave me a chance to focus on tree recursion, queue-based traversal, and brute-force string matching logic.
Letβs dive into each problem with clear code walkthroughs, visual breakdowns, and dry-run explanations. As always, Iβm sharing visuals and my approach to help you master these concepts too.
β Problems Solved:
Binary Tree Preorder Traversal (Recursive)
Binary Tree Level Order Traversal
Find the Index of the First Occurrence in a String
Revised: Delete Node in a Linked List & Remove Nth Node from End
β Problem 1: Binary Tree Preorder Traversal (Recursive)
π Problem Statement:
Traverse a binary tree in preorder: Root β Left β Right
.
π‘ Approach:
We use a helper method that recursively:
Visits the root.
Traverses the left subtree.
Traverses the right subtree.
π§ Code:
public void preorderhelper(TreeNode root, List out){
if(root == null) return;
out.add(root.val);
preorderhelper(root.left, out);
preorderhelper(root.right, out);
}
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
preorderhelper(root, out);
return out;
}
π§± Physical Representation:
1
/ \
2 3
/ \
4 5
Traversal Order: 1 β 2 β 4 β 5 β 3
π§Ύ Dry Run:
Call: preorder(1) β add(1)
Call: preorder(2) β add(2)
Call: preorder(4) β add(4)
Backtrack β preorder(5) β add(5)
Backtrack β preorder(3) β add(3)
β Problem 2: Find the Index of the First Occurrence in a String
π Problem Statement:
Given haystack
and needle
, return the index of the first occurrence of needle
in haystack
, or -1
if it doesnβt exist.
π‘ Approach:
We slide a window of size needle.length()
over haystack
and compare characters one by one.
π§ Code:
public int strStr(String haystack, String needle) {
if(haystack.length() < needle.length()) return -1;
for(int i = 0; i < haystack.length(); i++) {
int j = 0;
while(j < needle.length() && (i + j) < haystack.length()) {
if(needle.charAt(j) != haystack.charAt(i + j)) break;
j++;
}
if(j == needle.length()) return i;
}
return -1;
}
π§± Example:
haystack = "leetcode"
, needle = "code"
Check substrings:
"leet"
β"eetc"
β"etco"
β"tcod"
β"code"
β β returns 4
β Problem 3: Binary Tree Level Order Traversal (BFS)
π Problem Statement:
Return a list of node values level by level using Breadth-First Search.
π‘ Approach:
Use a
Queue
to process nodes.For each level:
Add all node values to a temporary list.
Push their children into the queue.
π§ Code:
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> out = new ArrayList<>();
if(root == null) return out;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()) {
int size = queue.size();
List<Integer> level = new ArrayList<>();
for(int i = 0; i < size; i++) {
TreeNode node = queue.remove();
level.add(node.val);
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
out.add(level);
}
return out;
}
π§± Physical Representation:
1
/ \
2 3
/ / \
4 5 6
Level Order Output:
[[1], [2, 3], [4, 5, 6]]
π Revised Problems
Today, I also revisited two important Linked List problems to solidify my learning. You can read my in-depth article for these below:
π Conclusion
Day 7 brought a perfect mix of string pattern recognition and tree traversal. The recursive depth of Preorder, the breadth-first magic of Level Order, and the precise logic behind String Matching made todayβs problems really rewarding.
π§ Lesson of the Day: A strong foundation in recursion and queue-based logic helps you tackle almost every tree-related problem in DSA!
π Want to follow my 90-day journey? Stay tuned every day for visual explanations, dry runs, and smart hacks to master DSA.
π Tags:
#DSA #90DaysChallenge #BinaryTrees #StringProblems #Java #CodingInterview #DailyDSA #LinkedList #Recursion #BFS #TechBlogger #CTOBhaiya
Subscribe to my newsletter
Read articles from Saivaishnav Ambati directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
