Mastering Java through LeetCode Day 37 of #LeetCode Challenge Today I solved Lowest Common Ancestor of a Binary Tree Problem Insight: The goal is to find the lowest node in a binary tree that has both given nodes as descendants (a node can be a descendant of itself). Approach I used: Used DFS + Recursion If the current node is null or matches one of the targets → return it Recursively search left and right subtrees If both sides return non-null → current node is the LCA ⚡ Key Learning: Understanding how recursion propagates results upward is crucial for solving tree problems efficiently. ⏱️ Complexity: Time: O(n) Space: O(h) Consistency > Intensity Improving problem-solving skills one day at a time! #DataStructures #Algorithms #Java #CodingJourney #SoftwareEngineering #OpenToWork #LeetCode #LearningInPublic
Java LeetCode Challenge: Lowest Common Ancestor of a Binary Tree
More Relevant Posts
-
🚀 Mastering Java Through LeetCode 🧠 Day 33 Not all nodes are “Good”… but finding them made me better at Trees 📌 LeetCode Problem Solved: Q.1448 – Count Good Nodes in Binary Tree 💭 Problem Summary: A node is called “Good” if no node on the path from root to it has a greater value. 🎯 Approach: ✔ Used DFS (Depth First Search) ✔ Tracked the maximum value seen so far in the path ✔ If current node ≥ maxSoFar → count it as a Good Node ✔ Updated max and continued recursion 🧠 Key Insight: Instead of storing the full path, just carry maxSoFar — simple yet powerful optimization! 💡 Complexity: ⏱ Time: O(N) 📦 Space: O(H) 🔥 What I Learned: How to maintain state during recursion Clean way to solve tree problems using DFS Thinking in terms of path-based conditions Consistency builds confidence 💪 Day 33 done… Let’s keep going 🚀 #Java #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #DSA #Tech #Learning
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Maximum Depth of Binary Tree 💻 Language: Java 🔹 Approach: To solve this problem, I used Breadth-First Search (BFS) with a Queue to perform level-order traversal of the binary tree. • Start by adding the root node to the queue • Traverse the tree level by level • For each level, process all nodes currently in the queue • Add left and right children of each node to the queue • Increment the level count after completing each level 📌 Logic Insight: Each iteration of the outer loop represents one level of the tree, which directly helps in calculating the maximum depth. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (queue storage in worst case) 💡 Key Learning: This problem helped me understand how level-order traversal (BFS) can be used not just for traversal, but also for solving structural problems like finding tree depth efficiently. It also reinforced the importance of queue-based processing in trees. #DSA #Java #LeetCode #BinaryTree #BFS #Queue #CodingJourney #ProblemSolving #LearningInPublic #TreeTraversal
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Same Tree 💻 Language: Java 🔹 Approach: To solve this problem, I used Recursion to compare both binary trees node by node. • If both nodes are null → return true • If one node is null and the other is not → return false • If node values are different → return false • Recursively compare left subtrees • Recursively compare right subtrees • If both left and right comparisons return true → both trees are identical This approach works because each subtree is itself a smaller version of the same problem, making recursion a natural fit. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(h) (where h = height of tree, worst case O(n)) 📖 Key Learning: This problem strengthened my understanding of tree traversal, recursion, and base case handling. It also reinforced how breaking a problem into smaller subproblems makes tree problems much easier to solve 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #Recursion
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: N-ary Tree Preorder Traversal (LeetCode 589) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to traverse the N-ary tree in preorder format. • Visit the root node first • Store the root value in the result list • Recursively traverse all children from left to right • Follow Preorder pattern: Root → Children This approach works efficiently because recursion naturally follows the preorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and N-ary tree traversal. It also reinforced the preorder pattern (Root → Children), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #NaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Inorder Traversal (LeetCode 94) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform inorder traversal of the binary tree. • Recursively traverse the left subtree • Visit the root node and store its value • Recursively traverse the right subtree • Follow Inorder pattern: Left → Root → Right This approach works efficiently because recursion naturally follows the inorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the inorder pattern (Left → Root → Right), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: N-ary Tree Postorder Traversal (LeetCode 590) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform postorder traversal of the N-ary tree. • Recursively traverse all child nodes first • Visit each child from left to right • Store the root node value at the end • Follow Postorder pattern: Children → Root This approach works efficiently because recursion naturally follows the postorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and N-ary tree traversal. It also reinforced the postorder pattern (Children → Root), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #NaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Postorder Traversal (LeetCode 145) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform postorder traversal of the binary tree. • Recursively traverse the left subtree • Recursively traverse the right subtree • Visit the root node and store its value • Follow Postorder pattern: Left → Right → Root This approach works efficiently because recursion naturally follows the postorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the postorder pattern (Left → Right → Root), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
CloudAEye is pleased to publish the 2026 open source benchmark report for code review. This benchmark is built on 50 real pull requests from five major open source projects, including Cal, Sentry, Keycloak, Discourse, and Grafana. The evaluation spans five programming languages: Python, TypeScript, Java, Go, and Ruby. Each review tool is assessed against human-verified golden comments that capture the issues a thorough reviewer is expected to catch. Every tool is measured on how effectively it identifies these issues and how much irrelevant noise it introduces alongside them. CloudAEye ranks #1 among 21 leading code review agents with an overall F1 score above 65 percent. Refer to the detailed report here: https://lnkd.in/gCs4-KPZ #CodeReview #AI #F1 #benchmark #AIDevTool
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Preorder Traversal (LeetCode 144) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform preorder traversal of the binary tree. • Visit the root node first • Store the root value in the result list • Recursively traverse the left subtree • Recursively traverse the right subtree • Follow Preorder pattern: Root → Left → Right This approach works efficiently because recursion naturally follows the preorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the preorder pattern (Root → Left → Right), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
Day 4 of Java with DSA Journey 🚀 📌 Topic: Search Insert Position (LeetCode 35) 💬 Quote: "Binary Search is about more than finding a needle in a haystack; it's about knowing exactly where to put a new needle." ✨ What I Learned: 🔹 Power of the low Pointer: If the target isn’t found, low directly gives the correct insertion index. 🔹 Finding Position Even When Missing: Binary Search doesn’t just search — it tells you where the element belongs. 🔹 Efficient Gap Detection: Even for missing values, we maintain O(log n) efficiency. 🔹 Complexity: ⏱ Time: O(log n) 📦 Space: O(1) 🧠 Problem Solved: ✔️ Search Insert Position 💡 Key Insight: Binary Search helps determine the rank/position of a number in a sorted array — whether it exists or not ⚡ ⚡ Interview Insight (Post-Loop Behavior): 👉 When the loop ends (low > high): low → first index greater than target high → last index smaller than target 🎯 That’s why low = insert position 🔑 Takeaway: Binary Search is not just about finding — it's about positioning. Consistency is the real key 🔑 #DSA #LeetCode #Java #CodingJourney #BinarySearch #ProblemSolving #100DaysOfCode #Algorithms #TechLearning #Day4 #Array #MCA #lnct
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