Day 10 of #50DaysOfLeetCode Challenge Just solved the "Leaf-Similar Trees" problem! This challenge was a perfect way to practice Depth First Search (DFS). The goal is to determine if two different binary trees have the same "leaf value sequence" when read from left to right. Key Takeaways: Tree Traversal: Used DFS to navigate down to the leaf nodes while maintaining the specific left-to-right order. Leaf Identification: A simple but effective check—a node is a leaf only if both its left and right children are null. Comparison Logic: Collecting the leaf sequences into lists and comparing them highlights how structural differences in trees don't always mean their "outputs" are different. #DataStructures #Algorithms #CodingJourney #Java #BinaryTree #DFS #LeetCode #TechLearning
Solved Leaf-Similar Trees with Depth First Search
More Relevant Posts
-
🚀Day 29 Solved LeetCode Problem #129 – Sum Root to Leaf Numbers Today I worked on a Binary Tree problem that focuses on DFS traversal and number formation from root-to-leaf paths. 🌳 🔹 Problem: Given a binary tree where each node contains digits (0–9), every root-to-leaf path forms a number. The goal is to calculate the total sum of all these numbers. 🔹 Example: Tree: [1,2,3] Paths: 1 → 2 = 12 1 → 3 = 13 Total Sum = 12 + 13 = 25 🔹 Approach: ✔ Use Depth First Search (DFS) ✔ Build the number while traversing the tree ✔ Multiply previous value by 10 and add the current digit ✔ When reaching a leaf node, add the number to the total sum ⏱ Time Complexity: O(n) 📦 Space Complexity: O(h) 💡 Key Learning: This problem helped strengthen my understanding of Binary Tree traversal and how recursive DFS can be used to carry state across nodes. Excited to keep improving my problem-solving skills! 💻🔥 #leetcode #datastructures #algorithms #java #binarytree #coding #softwaredeveloper #codingjourney
To view or add a comment, sign in
-
-
Day 2/30 — DSA Challenge 🚀 Problem: Lowest Common Ancestor of Deepest Leaves Topic: Tree + DFS Difficulty: Medium Approach: Used DFS to calculate depth of left and right subtrees At each node, compared depths: If equal → current node is LCA Else → return deeper subtree result Mistake / Challenge: Initially overcomplicated thinking about storing all deepest nodes Realized we don’t need to track nodes explicitly, depth comparison is enough Fix: Returned a Pair (node, depth) from recursion Handled everything in one DFS traversal Key Learning: Tree problems often look complex but can be simplified with recursion Focus on what needs to be returned from each call Time Taken: 60 minutes Consistency check ✅ See you on Day 3. GitHub Repo: https://lnkd.in/g87geK_g #DSA #LeetCode #Java #Trees #Recursion #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 90 – DSA Journey | Binary Tree Paths Continuing my daily DSA practice, today I explored how to track and construct paths in a binary tree. 📌 Problem Practiced: Binary Tree Paths (LeetCode 257) 🔍 Problem Idea: Return all root-to-leaf paths in a binary tree as strings. 💡 Key Insight: While traversing the tree, we can build the path step by step. Once we reach a leaf node, we add the complete path to the result. 📌 Approach Used: • Use DFS traversal • Maintain a string to track the current path • At each node, append its value to the path • If it’s a leaf node → add the path to result • Continue exploring left and right subtrees 📌 Concepts Strengthened: • Tree traversal (DFS) • Recursion • Path tracking • String manipulation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Tracking state (like a path) during recursion is a powerful technique for solving tree problems. On to Day 91! 🚀 #Day90 #DSAJourney #LeetCode #BinaryTree #DFS #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 09 of #50DaysOfLeetCode Challenge Just tackled the "Combination Sum" problem! This was a fantastic exercise in backtracking. The challenge is to find all unique combinations of numbers that sum up to a specific target, with the twist that you can use the same number multiple times. Key Insights: Backtracking Power: It’s all about exploring every possible path and "backtracking" as soon as the sum exceeds the target. State Space Tree: Visualizing how the recursion branches out helped me understand how to avoid duplicate combinations while allowing multiple uses of the same element. Decision Making: Learning when to include an element and when to move to the next index is crucial for optimizing the search. Each day, the logic gets sharper and the problems get more interesting! #DataStructures #Algorithms #CodingJourney #Java #Backtracking #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 2 Today I worked on a DSA problem based on arrays: Check if an array is sorted and rotated 🔍 Approach: Instead of finding the exact rotation point, I focused on identifying a pattern: In a sorted and rotated array, the order should break at most once. So, I checked how many times an element is greater than the next element while traversing the array in a circular manner. ✔️ If the count of such breaks is 0 or 1 → valid ❌ If it’s more than 1 → not a sorted rotated array 🧠 Key Takeaway: This problem taught me how pattern observation can simplify logic and avoid unnecessary complexity. Sometimes the best solution is not the most obvious one! 📈 Staying consistent and improving step by step 💪 #100DaysOfCode #DSA #DataStructures #Algorithms #Java #CodingJourney #ProblemSolving #LeetCode #Consistency
To view or add a comment, sign in
-
-
The High Cost of the Wrong Initial Choice I have seen complex computation logic for over a million records take hours to run simply because of inefficient, row-by-row processing that is offered by database stored procedures and traditional java/python code. It is slow, impossible to scale, and eventually kills your product's edge in the market. By moving to vectorized logic with tools like NumPy or Polars, you can turn those hours of computation into milliseconds. This is not just about using newer tools, it is about leveraging modern execution like #SIMD (Single Instruction Multiple Data) and #multithreading to gain a massive competitive advantage. If your backend is computation heavy and you're still stuck in legacy loops, you are leaving performance, market edge and money on the table. References: https://lnkd.in/g8j3A5Bn https://lnkd.in/gaPnFUQm https://lnkd.in/gWt9WHnp #DataEngineering #SystemDesign #NumPy #PerformanceOptimization #Scalability #PythonProgramming #TechStrategy #Vectorization #TechToolChoice
To view or add a comment, sign in
-
💡 Day 56 of LeetCode Problem Solved! 🔧 🌟 Mirror of an Integer 🌟 🔗 Solution Code: https://lnkd.in/gvaEY4Sw 🧠 Approach: • Digit Extraction & Reversal Extract digits from right to left using modulo (% 10) and rebuild the reversed number dynamically. • Calculate the absolute difference abs(n - reversed) at the end to find the mirror distance. ⚡ Key Learning: • Mastering number manipulation with fundamental math operators (% and /) is a game-changer for processing integers efficiently without relying on space-consuming String conversions. ⏱️ Complexity: • Time: O(log₁₀(n)) (proportional to the number of digits) • Space: O(1) #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfCode #CodingJourney #MathLogic #DataStructures
To view or add a comment, sign in
-
-
## 🧩 Solved LeetCode 9 – Palindrome Number Today I revisited a fundamental integer manipulation problem: determining if a number reads the same forward and backward without converting it to a string. 🔍 **Problem Insight:** While string conversion makes this trivial, the real challenge is solving it mathematically. The goal is to reverse the integer (or half of it) and compare it to the original while being mindful of potential integer overflow. 💡 **Key Learnings:** * **Negative Numbers:** Any negative number is immediately invalid (e.g., -121 becomes 121-). * **Mathematical Reversal:** Using modulo % 10 and integer division / 10 allows us to "pop" and "push" digits. * **Optimization:** We only need to reverse **half** of the number. Once the original number becomes less than or equal to the reversed half, we've reached the middle. * **Edge Case Savvy:** Numbers ending in 0 (except 0 itself) cannot be palindromes. ⚙️ **Approach Used:** 1. Eliminate negative numbers and numbers ending in 0. 2. Build the reversed half of the number by iteratively taking the last digit. 3. Stop when the reversed part is \ge the remaining part. 4. Check for equality (handling odd-lengthed numbers by dividing the reversed part by 10). 📊 **Complexity:** * **Time:** O(\log_{10}(n)) — we divide the input by 10 in every iteration. * **Space:** O(1) — constant space used regardless of input size. 🔥 **Takeaway:** This problem is a perfect example of why we shouldn't always reach for the easiest data type conversion. Thinking in terms of pure logic and math often leads to a more memory-efficient and elegant solution. #LeetCode #Algorithms #Math #ProblemSolving #CodingJourney #Java #DataStructures #CompetitiveProgramming
To view or add a comment, sign in
-
-
Day 30/160 of my DSA journey Today I solved the Search in a Rotated Sorted Array problem. Key Insight: Even after rotation, at least one half of the array is always sorted. Using this property, we can decide which side to search using Binary Search. Approach: • Found mid element using binary search • Checked which half is sorted (left or right) • If left half is sorted → checked if key lies in that range • Else → searched in right half • Repeated until element found or search space exhausted Concepts reinforced: • Binary Search on rotated arrays • Identifying sorted halves • Conditional search space elimination Time Complexity: O(log n) ⚡ Another step forward in problem solving 🚀 #Day30 #160DaysChallenge #DSA #GeeksforGeeks #LeetCode #CodingJourney #ProblemSolving #Algorithms #Java
To view or add a comment, sign in
-
-
#Day77 of my second #100DaysOfCode Finally wrapped up binary search on 1D arrays, now moving to binary search on answers. DSA • Solved Sqrt(x) (LeetCode 69) — finding the integer square root of a number without using built-in functions – Brute: iterate until square exceeds target → O(n) – Optimal: binary search on answer space → O(log n) • Key idea: instead of searching an index, we search for the answer itself • Learned how to narrow down the range based on mid² comparison Nice shift in thinking — binary search feels much more powerful now. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
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