Day 32/100 – Diameter of Binary Tree Today’s problem looked simple on the surface, but it really tested my understanding of tree traversal and recursion. Key Insight: The diameter of a binary tree isn’t about paths from root—it’s about the longest path between any two nodes. The trick? At every node, calculate: Left subtree height Right subtree height Update diameter = left + right This turns a potentially complex problem into a smooth DFS + height calculation. What I learned: How to combine recursion with global state Thinking beyond root-based solutions Every node can be the "center" of the longest path Time Complexity: O(n) Space Complexity: O(h) Consistency > intensity. On to Day 33 #100DaysOfCode #DataStructures #Algorithms #LeetCode #Python #CodingJourney
Binary Tree Diameter with DFS and Recursion
More Relevant Posts
-
Day 254 of #365DaysOfCode Solved Check if There is a Valid Path in a Grid using a DFS-based traversal with directional constraints. Each cell type defines allowed movement directions, and transitions are validated by ensuring bidirectional connectivity between adjacent cells. The traversal explores only feasible paths while maintaining a visited structure to avoid cycles. The solution runs in O(n × m) time and emphasizes careful handling of constrained graph traversal. Continuing to strengthen understanding of grid-based graphs and state validation. #365DaysOfCode #Day254 #DSA #LeetCode #Python #Algorithms #DFS #Graphs #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Solved Kth Smallest Element in a BST today Used an iterative inorder traversal (DFS with stack) to efficiently find the answer without extra space for storing nodes. Key idea: 👉 Inorder traversal of a BST gives nodes in sorted order 👉 So the k-th visited node is the answer Instead of recursion, I used a stack to simulate traversal: Go left as much as possible Process node Move right Clean, efficient, and interview-friendly ✅ Time: O(H + k) Space: O(H) (stack) Consistent practice is making these patterns feel natural 🚀 #LeetCode #DataStructures #Algorithms #Python #CodingInterview
To view or add a comment, sign in
-
-
✅ Day 28 of #DSAPrep > Problem: Rearrange Array Elements by Sign > Platform: LeetCode > Concept: Array / Two Pointers Solved this problem using a two-pointer approach to place positive and negative numbers alternately in a new array. > Key Idea: Create a result array of same size Use one pointer for positive index (0,2,4...) Use another pointer for negative index (1,3,5...) Traverse input array and place elements accordingly > Time Complexity: O(n) > Space Complexity: O(n) #DSAPrep #Algorithms #Python #Arrays #TwoPointers #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Subsets: Backtracking with Include/Exclude Decision Tree Generate all 2^n subsets via recursive binary choices — include current element or skip. Base case: processed all elements, save current subset. Backtracking pattern: modify state, recurse, undo modification. Backtracking Pattern: Modify shared state, explore branch, restore state before exploring alternate branch. This template applies to permutations, combinations, constraint satisfaction problems. Time: O(2^n) | Space: O(n) recursion depth #Backtracking #Subsets #DecisionTree #StateRestoration #Recursion #Python #AlgorithmDesign #SoftwareEngineering
To view or add a comment, sign in
-
-
Binary Tree Level Order Traversal: Queue with Level Isolation BFS naturally traverses level-by-level. Key technique: snapshot queue length before processing current level. Process exactly that many nodes, collecting values into level list while enqueueing children for next iteration. This prevents mixing levels. Level Isolation: Pre-loop length snapshot prevents newly-added children from affecting current level's iteration count. This pattern enables clean level-by-level processing. Time: O(n) | Space: O(w) where w = max width #BFS #LevelOrderTraversal #QueuePattern #TreeAlgorithms #Python #AlgorithmDesign #SoftwareEngineering
To view or add a comment, sign in
-
-
🗓 7 April 2026 LeetCode Problem #128 – Longest Consecutive Sequence Solved the problem of finding the longest consecutive sequence in an unsorted array. Key insight: use a set for O(1) lookups and only start counting sequences from numbers that are the beginning of a sequence. Takeaways: - Using the right data structure reduces time complexity from O(n²) to O(n). - Avoid redundant work while scanning arrays. - Handle edge cases like empty or single-element arrays efficiently. This problem reinforces how a smart approach beats brute force every time! #LeetCode #Algorithms #Python #DataStructures #ProblemSolving #Coding #TechLearning
To view or add a comment, sign in
-
-
𝗧𝗵𝗿𝗲𝗲 𝗻𝘂𝗺𝗯𝗲𝗿𝘀. 𝗢𝗻𝗲 𝗽𝗮𝘀𝘀. 𝗢𝗻𝗲 𝗴𝗵𝗼𝘀𝘁 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲. Day 28 of #1000DaysOfLearning Today's problem: 𝗦𝗼𝗿𝘁𝗲𝗱 𝗦𝘂𝗯𝘀𝗲𝗾𝘂𝗲𝗻𝗰𝗲 𝗼𝗳 𝗦𝗶𝘇𝗲 𝟯 (GFG Medium) Find arr[i] < arr[j] < arr[k] where i < j < k — in a single pass. Track the smallest first, then the smallest second after it. The moment you see x > second, you've got your triplet. The tricky part: if first gets updated 𝗮𝗳𝘁𝗲𝗿 second is already set, it's no longer positionally before second. So you keep a prevFirst — 𝘁𝗵𝗲 𝘃𝗮𝗹𝘂𝗲 first 𝗵𝗲𝗹𝗱 𝘄𝗵𝗲𝗻 second 𝘄𝗮𝘀 𝗹𝗮𝘀𝘁 𝘂𝗽𝗱𝗮𝘁𝗲𝗱. Even if first moves to something smaller later, prevFirst still holds the element that actually appeared before second in the array. That's what makes the returned triplet 𝗽𝗼𝘀𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝗹𝘆 𝘃𝗮𝗹𝗶𝗱, not just value-valid. 𝗢(𝗻) time. 𝗢(𝟭) space. #DSA #Python #DataScience #GFG #1000DaysOfLearning #LearningInPublic
To view or add a comment, sign in
-
-
Find Minimum in Rotated Array: Binary Search with Rotation Detection Rotation breaks global order but one half stays sorted. Compare mid with right endpoint — if mid > right, minimum is in right half (rotation there). Otherwise, minimum in left or at mid. Track minimum while narrowing. Rotation Point Detection: Comparing mid with endpoint determines which half contains rotation/minimum. This partial ordering enables O(log n) despite global disruption. Time: O(log n) | Space: O(1) #BinarySearch #RotatedArray #MinimumFinding #RotationPoint #Python #AlgorithmDesign #SoftwareEngineering
To view or add a comment, sign in
-
-
Next stop: Support Vector Machines (SVM)! 🚀 When data patterns become too complex for Linear or Logistic Regression to decode, it’s time to bring in a more powerfully. I’ve just implemented an SVM (Support Vector Machine) model to tackle the classic challenge of handwritten digit recognition. Using a dataset of journey. Onward to the next challenge! #MachineLearning #DataScience #SVM #ArtificialIntelligence #Python #ComputerVision #CodingJourney #LinkedInLearning
To view or add a comment, sign in
-
Most RAG pipelines fail before they ship. Not because of the model. Because of the retrieval. I fixed the following in production: → Hybrid search with BM25 → Added metadata filters → Chunked documents by section → Reranked top results Latency dropped significantly. Groundedness improved substantially. The model was not the issue. The pipeline was the problem. Save this if you are building RAG. #RAG #AIEngineering #LLM #Python #BuildInPublic
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