🚀 Just Solved LeetCode #102 — Binary Tree Level Order Traversal 📘 Problem: Given the root of a binary tree, return the level order traversal of its nodes' values — meaning, traverse the tree level by level from left to right. Example: Input → [3,9,20,null,null,15,7] Output → [[3], [9,20], [15,7]] 🧠 My Approach: I solved this using a recursive approach instead of the usual queue-based BFS. 1️⃣ First, I calculated the total number of levels in the tree using recursion. 2️⃣ Then, for each level, I called a helper function `nThLevel()` to collect all nodes at that level. 3️⃣ I stored each level’s nodes in a separate list and finally returned a list of lists as the result. This approach helped me deeply understand the relationship between recursion depth and tree levels. 💡 What I Learned: ✅ Different ways to perform level order traversal — BFS vs recursion. ✅ How to effectively use recursion to explore each tree level. ✅ Improved understanding of function calls, base conditions, and data storage in recursive algorithms. #LeetCode #Java #DSA #BinaryTree #CodingJourney
Solved LeetCode #102 with Recursive Approach
More Relevant Posts
-
#100DaysOfCode – Day 76 Insert a Node in a Doubly Linked List Problem: Given a position p and a value x, insert a new node with data x after the p-th node (0-based indexing) in a doubly linked list. Example: Input: p = 2, x = 6 Output: 2 <-> 4 <-> 5 <-> 6 My Approach: Traversed to the p-th node using a simple loop. Updated pointers of the prev and next nodes carefully to maintain bidirectional linkage. Handled edge cases for insertion at the end of the list. Time Complexity: O(N) Space Complexity: O(1) Understanding pointer manipulation in linked lists builds a strong base for advanced data structure problems like deletion, reversal, and flattening of linked lists. #takeUforward #100DaysOfCode #DSA #Java #ProblemSolving #GeeksForGeeks #LinkedList #Pointers #CodingChallenge #CodeNewbie #LearningEveryday
To view or add a comment, sign in
-
-
🎯 Day 82 of #100DaysOfCode – LeetCode 137: Single Number II Today’s challenge was about identifying the unique number in an array where every element appears three times except for one. 🧩 Problem Statement: Given an integer array nums, where every element appears exactly three times except for one, find that single element that appears only once. Example: Input: [2,2,3,2] Output: 3 💭 Approach 1: Using HashMap At first, I solved this using a HashMap to count the frequency of each number. Traverse the array and store each element’s occurrence. Return the element whose count equals 1. 🕒 Time Complexity: O(n) 💾 Space Complexity: O(n) 🚀 Key Takeaway: Sometimes, the best way to optimize is not by adding more data structures, but by understanding how data behaves at the bit level. #100DaysOfCode #Day82 #LeetCode #Java #ProblemSolving #DSA #CodingJourney #BitManipulation #KeepLearning
To view or add a comment, sign in
-
-
🚀 Just implemented a custom Vector<T> class in Java — a dynamic array structure that grows automatically as elements are added. It supports key operations like push_back, pop_back, front, back, clear, and more — all while practicing the concepts of generics, dynamic memory allocation, and data encapsulation. Rebuilding core data structures from scratch is a great way to understand how tools like ArrayList work internally. 💻 #Java #DSA #LearningByDoing #CodingJourney #DataStructures Nohit Singh,Shreyash Khedekar,Anshuman Singh,Arpit Jain
To view or add a comment, sign in
-
📌 Day 38/100 – Make array elements equal to zero (LeetCode 3354) 🔹 Problem: Given an integer array nums, each element can be a number or zero. You need to find how many zeros in the array can be replaced by either +1 or -1 such that the total sum on both sides of that zero (left and right) remains balanced or differs by 1. 🔹 Approach: First, calculate the total sum s of all elements. Maintain a prefix sum l as you iterate. For each zero: If l * 2 == s, both +1 and -1 replacements are valid → add 2 to ans. If |l * 2 - s| == 1, only one replacement is valid → add 1 to ans. Return the total count ans. 🔹 Key Learning: Prefix sums simplify balance-based problems. Comparing 2 * prefixSum with total sum helps quickly check left-right equilibrium. 🔹 Complexity: Time: O(n) — single pass through array Space: O(1) — no extra storage used 🔹 Hashtags: #Day38Of100 #LeetCode3354 #100DaysOfCode #Java #DSA #ProblemSolving #PrefixSum #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #124 — Binary Tree Maximum Path Sum 📘 Problem: Given the root of a binary tree, return the maximum path sum of any non-empty path. A path can start and end at any node, and it’s the sum of all node values along that path. Example: Input → [1,2,3] Output → 6 Explanation → The optimal path is 2 → 1 → 3 with a sum of 6. 🧠 My Approach: I used a recursive DFS (Depth First Search) to calculate the maximum gain from each node: 1️⃣ For each node, calculate the maximum path sum including its left and right subtrees. 2️⃣ Compare the sum of the current path with the global maximum (`maxSum`). 3️⃣ Return the max gain that can be extended to the parent node (node value + max of left/right gain). 4️⃣ Used `Math.max()` to ensure negative paths don’t reduce the result. 💡 What I Learned: ✅ Importance of returning gain vs total path sum in recursive tree problems ✅ How to handle global state (`maxSum`) in recursion ✅ Deepened understanding of DFS-based tree traversal and dynamic recursion logic #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Progress Update: Invert Binary Tree (Problem 226) 🌳 About the Problem: The task was to invert a binary tree — flipping it by swapping every left and right child node. It’s a classic problem that tests recursion and tree traversal logic. 🧠 My Approach: I went with a recursive approach. At each node, I swapped the left and right subtrees, then called the same logic for both sides. Once the node became null, the recursion stopped automatically. 💡 What I Learned: ✅ How recursion travels through a tree structure ✅ Why defining a clear base condition matters ✅ How simple logic can transform an entire data structure 🎯 Takeaway: This problem reinforced that not every challenge needs complexity — sometimes, the cleanest recursive idea is the smartest one. #LeetCode #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 77 Delete in a Doubly Linked List Problem: Given a doubly linked list and an integer x, remove the node at position x (1-indexed) and return the head of the updated list. Example: Input: 1 <-> 2 <-> 3 <-> 4, x = 3 Output: 1 <-> 2 <-> 4 My Approach: 1️⃣ Handled edge cases deleting the head or an empty list. 2️⃣ Traversed to the node at position x. 3️⃣ Updated both prev and next pointers to unlink the node cleanly. Time Complexity: O(N) Space Complexity: O(1) Understanding pointer manipulation in linked lists builds the foundation for mastering advanced data structures. Every prev and next link matters! #100DaysOfCode #Java #ProblemSolving #DSA #GeeksforGeeks #LinkedList #Pointers #TakeUForward #CodeNewbie #CodingJourney
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #1367 — Linked List in Binary Tree 📘 Problem: Given the head of a linked list and the root of a binary tree, determine whether all the elements of the linked list appear as a downward path in the binary tree. Downward means starting from any node and moving only to child nodes. Example: Input → head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] Output → true Explanation → Nodes in blue form a subpath in the binary tree. 🧠 My Approach: I used recursion to check whether the linked list sequence can be found starting from any node in the binary tree. 1️⃣ For each node in the tree, check if the list starting from `head` matches the downward path from that node. 2️⃣ If not, recursively check the left and right subtrees. 3️⃣ Used a helper function `checkPath()` to verify if a valid path continues as the recursion goes deeper. 💡 What I Learned: ✅ How to combine linked list and binary tree traversal logic ✅ How recursion can efficiently check multiple starting points ✅ Improved my understanding of tree path traversal and backtracking logic #LeetCode #Java #DSA #BinaryTree #LinkedList #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
Day 41 of 160 Days of DSA 🚀 Topic: Set Matrix Zeroes Today, I learned a valuable lesson while solving the Set Matrix Zeroes problem — sometimes the order in which we update data matters more than the logic itself. The core idea of the problem: If any element in the matrix is 0, we have to set its entire row and column to 0. At first, I tried to directly modify the matrix during traversal. But this caused new zeroes to appear, which led to overwriting more rows/columns than required. So the correct approach is: First pass → Scan the matrix and record which rows and columns should be zeroed. Second pass → Set those rows and columns to zero. This prevents us from modifying the matrix while still evaluating it. Key Takeaway: When transforming a data structure based on its own values, sometimes the right answer requires separating detection from modification. #100DaysOfCode #geeksforgeeks #dsa #java #arrays #learningEveryday #codingjourney #logicbuilding
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development