🚀 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
Symmetric Tree Solution on LeetCode
More Relevant Posts
-
Day 72 of #100DaysOfCode Problem: Convert Sorted Array to Height-Balanced BST Today I learned how to efficiently convert a sorted array into a balanced Binary Search Tree using Divide & Conquer. Key Insight: Pick the middle element as the root to maintain balance. Recursively build: Left subtree from left half Right subtree from right half This ensures: Optimal height Faster search operations ⏱ Time Complexity: O(n) 📦 Space Complexity: O(log n) Consistency is the real game changer #DSA #Java #BinaryTree #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 64 of #100DaysOfCode Solved 222. Count Complete Tree Nodes on LeetCode 🔗 🧠 Key Insight: In a complete binary tree, all levels are fully filled except possibly the last, and nodes are as left as possible. 👉 This property helps us optimize beyond simple traversal ⚙️ Approach (Simple DFS - Your Solution): 1️⃣ If root is null → return 0 2️⃣ Recursively count: 🔹 left = countNodes(root.left) 🔹 right = countNodes(root.right) 3️⃣ Total nodes: 👉 1 + left + right ⏱️ Time Complexity: Current → O(n) Optimized → O(log² n) 📦 Space Complexity: O(h) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DivideAndConquer #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟔 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding a peak element using binary search. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Peak Element 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used binary search instead of linear scan • Compared the middle element with its next element Logic: • If nums[mid] > nums[mid + 1] → peak lies on the left side (including mid) • Else → peak lies on the right side • Continued until left == right 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Binary search can be applied on patterns, not just sorted arrays • A peak always exists due to problem constraints • Comparing adjacent elements helps determine direction • Reducing the search space is the key idea 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is not about sorted arrays — it’s about eliminating half of the search space using logic. 56 days consistent 🚀 On to Day 57. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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
-
-
𝐃𝐚𝐲 𝟖𝟕/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐒𝐡𝐨𝐫𝐭𝐞𝐬𝐭 𝐃𝐢𝐬𝐭𝐚𝐧𝐜𝐞 𝐭𝐨 𝐓𝐚𝐫𝐠𝐞𝐭 𝐒𝐭𝐫𝐢𝐧𝐠 𝐢𝐧 𝐚 𝐂𝐢𝐫𝐜𝐮𝐥𝐚𝐫 𝐀𝐫𝐫𝐚𝐲 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Iterate through the array to find all occurrences of the target. For each index, compute: Direct distance → |i - startIndex| Circular distance → n - |i - startIndex| Take the minimum of both. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Linear scan + circular distance calculation. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝟏) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: In circular structures, always consider both forward and wrap-around paths. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Arrays #Strings #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🚀 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 72/100 Completed ✅ 🚀 Solved LeetCode – Matrix Diagonal Sum (Java) ⚡ Implemented an efficient approach to calculate the sum of both primary and secondary diagonals of a square matrix in a single traversal. Avoided double-counting the center element by handling the odd-sized matrix case separately. 🧠 Key Learnings: • Efficient diagonal traversal in a matrix • Handling overlapping elements (center in odd n) • Writing optimal O(n) solutions instead of nested loops • Clean and concise index-based logic 💯 This problem improved my understanding of matrix patterns and how to optimize traversal by reducing unnecessary iterations. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #arrays #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
🚀 Day 84/100 – 𝐒𝐞𝐚𝐫𝐜𝐡 𝐢𝐧 𝐑𝐨𝐭𝐚𝐭𝐞𝐝 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 𝐈𝐈 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Binary search works great on sorted arrays, but duplicates introduce ambiguity — making it harder to decide which half is sorted. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Use modified binary search Identify the sorted half Handle duplicates by shrinking the search space ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Find 𝐦𝐢𝐝 If 𝐭𝐚𝐫𝐠𝐞𝐭 𝐟𝐨𝐮𝐧𝐝 → 𝐫𝐞𝐭𝐮𝐫𝐧 𝐭𝐫𝐮𝐞 𝐇𝐚𝐧𝐝𝐥𝐞 𝐝𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 (𝐥𝐨𝐰++) Check which half is sorted Narrow down search accordingly #Day84 #100DaysOfCode #Java #DSA #LeetCode #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
Day 70 - Next Greater Node Understanding how to find the next larger value for each node using stack optimization. Approach: • Convert linked list → array for easy indexing • Use a stack to track indices • Compare current value with stack top • Update results when a greater value is found Key Insight: Monotonic stack helps avoid unnecessary comparisons Time Complexity: O(n) Space Complexity: O(n) #Day70 #LeetCode #Java #CodingPractice #TechJourney #DSA #LinkedList
To view or add a comment, sign in
-
-
🚀 Day 75 of #100DaysOfCode Solved 103. Binary Tree Zigzag Level Order Traversal on LeetCode 🔗 🧠 Key Insight: This is a variation of level order traversal where: 👉 Levels alternate between left → right and right → left ⚙️ Approach (BFS + Direction Toggle): 1️⃣ Use a queue for level order traversal 2️⃣ Maintain a flag (leftToRight or sign) 🔹 true → left → right 🔹 false → right → left 3️⃣ For each level: 🔹 Create a list 🔹 Traverse all nodes in that level 4️⃣ Insert values based on direction: 🔹 If left → right → addLast(val) 🔹 Else → addFirst(val) 5️⃣ Add level list to result 6️⃣ Flip direction: 👉 sign *= -1 or toggle boolean ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #Queue #Java #InterviewPrep #CodingJourney
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