🚀 Day 70 of #100DaysOfCode Solved 107. Binary Tree Level Order Traversal II on LeetCode 🔗 🧠 Key Insight: This is just level order traversal (BFS) but: 👉 Return levels from bottom to top instead of top to bottom ⚙️ Approach (DFS with Level Tracking): 1️⃣ Start traversal with level = 0 2️⃣ For each node: 🔹 If level not present → insert new list at front 🔹 Add value at correct position: 👉 res.get(res.size() - 1 - level) 3️⃣ Recurse: 🔹 Left → level + 1 🔹 Right → level + 1 ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #DFS #Java #InterviewPrep #CodingJourney
Solved 107. Binary Tree Level Order Traversal II on LeetCode
More Relevant Posts
-
🚀 Day 60 of #100DaysOfCode Solved 100. Same Tree on LeetCode 🔗 🧠 Key Insight: Two trees are the same if: 👉 Their structure is identical 👉 Corresponding nodes have equal values ⚙️ Approach (Recursive DFS): 1️⃣ If both nodes are null → ✅ return true 2️⃣ If one is null and other is not → ❌ return false 3️⃣ If values are different → ❌ return false 4️⃣ Recursively check: 🔹 Left subtree → isSame(p.left, q.left) 🔹 Right subtree → isSame(p.right, q.right) 5️⃣ Return: 👉 leftCheck && rightCheck ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursive stack) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀Day 40 LeetCode 1161 – Maximum Level Sum of a Binary Tree Ever wondered how to find the level of a binary tree with the maximum sum of node values? 🤔 Here’s the idea: We traverse the tree level by level (BFS) and compute the sum at each level. The trick is to keep track of the maximum sum and return the smallest level where this occurs. 💡 Key Insight: Level-order traversal (using a queue) naturally processes nodes one level at a time, making it perfect for this problem. 🔧 Approach: ✔ Use a queue for BFS ✔ For each level: • Calculate sum of nodes ✔ Track maximum sum and corresponding level 🔥 Takeaway: Whenever a problem involves levels in a tree, think BFS first — it often leads to the cleanest solution. #LeetCode #DataStructures #Java #CodingInterview #BinaryTree #BFS #ProblemSolving
To view or add a comment, sign in
-
-
Day 41 of Daily DSA 🚀 Solved LeetCode 20: Valid Parentheses ✅ Problem: Given a string containing only (), {}, [], determine if the input string is valid. Rules: Open brackets must be closed by the same type Open brackets must be closed in the correct order Every closing bracket must have a matching opening bracket Approach: Used a Stack to track opening brackets and validate matching pairs. Steps: Traverse the string Push opening brackets onto the stack For closing brackets → check top of stack If it matches → pop Else → return false At the end, stack should be empty ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 3 ms (Beats 87.41%) ⚡ • Memory: 43.37 MB A classic stack problem that builds strong fundamentals for expression parsing & validation. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 78 - Binary Tree Level Order Traversal Implemented level-by-level traversal of a binary tree using a queue (BFS). Approach: • Use a queue to process nodes • Traverse level by level • Track size of each level • Store values accordingly Time Complexity: O(n) Space Complexity: O(n) #Day78 #LeetCode #BinaryTree #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 82 – Univalued Binary Tree Solved a problem to determine whether all nodes in a binary tree have the same value using depth-first traversal. Key Learnings: Applied DFS recursion to traverse all nodes in the tree Compared each node’s value with the root value for validation #DSA #Java #BinaryTree #DFS #Recursion #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
Day 6/75 – Data Structures & Algorithms Practice Solved the “Best Time to Buy and Sell Stock” problem on LeetCode using Java. • 212 / 212 test cases passed • Runtime: 1 ms (Beats 99.92%) • Time Complexity: O(n) • Space Complexity: O(1) Approach: Used a single-pass greedy strategy by tracking the minimum price so far and continuously updating the maximum profit. This avoids checking all pairs and keeps the solution efficient. Key Takeaways: Importance of optimizing brute-force logic Writing clean and efficient code Strengthening understanding of greedy techniques Consistent daily problem-solving is helping me improve both logic building and performance optimization. #75DaysOfDSA #DSA #Java #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Some of the hardest problems become manageable once you recognize a repeating pattern. 🚀 Day 105/365 — DSA Challenge Solved: Subarrays with K Different Integers Problem idea: We need to count subarrays that contain exactly k distinct integers. Efficient approach: Use the powerful trick: subarrays with exactly k distinct = subarrays with ≤ k distinct − subarrays with ≤ (k − 1) distinct Steps: 1. Use a sliding window with a hashmap to track frequency of elements 2. Expand window by moving right pointer 3. If distinct count exceeds k, shrink window from the left 4. Count valid subarrays ending at each index 5. Subtract results to get exact count This pattern converts a hard problem into a manageable one. ⏱ Time: O(n) 📦 Space: O(n) Day 105/365 complete. 💻 260 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #HashMap #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 61 of #100DaysOfCode Solved 101. Symmetric Tree on LeetCode 🔗 🧠 Key Insight: A tree is symmetric if it is a mirror of itself 👉 Left subtree is a mirror of right subtree ⚙️ Approach (Recursive Mirror Check): 1️⃣ Start by comparing: 🔹 root.left and root.right 2️⃣ If both are null → ✅ symmetric 3️⃣ If one is null → ❌ not symmetric 4️⃣ If values differ → ❌ not symmetric 5️⃣ Recursively check mirror condition: 🔹 left.left with right.right 🔹 left.right with right.left 6️⃣ Return: 👉 isMirror(left.left, right.right) && isMirror(left.right, right.left) ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🧠 Day 36/75: Recursion within Recursion! Today’s challenge was "Subtree of Another Tree," a problem that perfectly illustrates how we can reuse fundamental patterns to solve more complex structures. The Strategy: To solve this, I combined two recursive ideas: Traverse the Main Tree: A standard DFS to visit every node in the primary tree. Structural Comparison: At every node, I used a helper function (isSameTree) to check if the current subtree perfectly matches the target structure. Performance: ✅ Runtime: 3 ms (Beats 67.99% of Java users) ✅ Complexity: $O(M \times N)$ in the worst case, but highly efficient for most tree structures. It’s incredibly satisfying to see yesterday's "Same Tree" logic become just one small part of today's larger solution. Build, repeat, and scale! 🚀 #LeetCode #75DaysOfCode #Java #BinaryTree #Recursion #DSA #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
Problem :- Plus One (LeetCode 66) Problem Statement :- You are given a large integer represented as an integer array digits, where each digits[i] is a digit of the integer. The digits are ordered from most significant to least significant. Increment the integer by one and return the resulting array of digits. Approach :- Carry Handling i - Traverse from the last digit ii - If digit < 9 → increment and return iii - If digit == 9 → make it 0 and carry forward iv - If all digits are 9 → create new array v - Time Complexity : O(n) vi - Space Complexity : O(1) class Solution { public int[] plusOne(int[] digits) { int n = digits.length; for(int i = n - 1; i >= 0; i--) { if(digits[i] < 9) { digits[i]++; return digits; } digits[i] = 0; } int[] result = new int[n + 1]; result[0] = 1; return result; } } How would you optimize this further? #Java #DSA #LeetCode #CodingJourney #LearnInPublic #SoftwareEngineering #Arrays
To view or add a comment, sign in
-
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