🚀 Day 115 of #LeetCode Challenge! Problem: Symmetric Tree 🌳 Concept: Check whether a binary tree is a mirror of itself (i.e., symmetric around its center). 💡 My Approach: Use recursion to compare left and right subtrees. A tree is symmetric if: Left subtree of left node matches right subtree of right node Right subtree of left node matches left subtree of right node Base cases: Both nodes NULL → symmetric ✅ One NULL and one not → not symmetric ❌ Values must match ✨ Example: Input: [1,2,2,3,4,4,3] Output: true ⏱ Time Complexity: O(N) — visiting all nodes 📦 Space Complexity: O(H) — recursion stack (H = height of tree) 📌 Key Insight: This is a classic mirror check — left's left must mirror right's right! 👨💻 GitHub Link: https://lnkd.in/gid6xjvn #LeetCode #BinaryTree #Recursion #SymmetricTree #MirrorTree #DSA #ProblemSolving #C++ #CodingChallenge #Day114
How to check if a binary tree is symmetric using recursion
More Relevant Posts
-
🚀 Day 114 of #LeetCode Challenge! Problem: Symmetric Tree 🌳 Concept: Check whether a binary tree is a mirror of itself (i.e., symmetric around its center). 💡 My Approach: Use recursion to compare left and right subtrees. A tree is symmetric if: Left subtree of left node matches right subtree of right node Right subtree of left node matches left subtree of right node Base cases: Both nodes NULL → symmetric ✅ One NULL and one not → not symmetric ❌ Values must match ✨ Example: Input: [1,2,2,3,4,4,3] Output: true ⏱ Time Complexity: O(N) — visiting all nodes 📦 Space Complexity: O(H) — recursion stack (H = height of tree) 📌 Key Insight: This is a classic mirror check — left's left must mirror right's right! 👨💻 GitHub Link: https://lnkd.in/grXFRA2i #LeetCode #BinaryTree #Recursion #SymmetricTree #MirrorTree #DSA #ProblemSolving #C++ #CodingChallenge #Day114
To view or add a comment, sign in
-
-
🚀 Day 125 of #LeetCode Challenge! Problem: Binary Tree Preorder Traversal 💡 My Approach: The goal is to perform a preorder traversal of a binary tree — visiting nodes in the order: Root → Left → Right Here’s the step-by-step logic: Start from the root node. Visit (record) the root value first. Recursively traverse the left subtree, then the right subtree. Store values in a vector as you go. ✨ Example Input: [1, null, 2, 3] Output: [1, 2, 3] 🧠 Key Idea Preorder traversal is useful when you need to copy or serialize a tree — it captures the structure starting from the root. ⏱ Complexity TypeValueTimeO(N) — each node visited onceSpaceO(H) — recursion stack (H = height of tree) 📎 GitHub Link: https://lnkd.in/gZ6GeXzm #LeetCode #BinaryTree #PreorderTraversal #Recursion #DSA #C++ #ProblemSolving #CodingChallenge #Day125
To view or add a comment, sign in
-
-
🚀 Day 124 of #LeetCode Challenge! Problem: Binary Tree Inorder Traversal 💡 My Approach: The goal is to perform an inorder traversal of a binary tree — visiting nodes in the order: Left → Root → Right. Here’s how I implemented it: Use recursion to traverse the tree. Start from the root node. Recursively visit the left subtree, then record the root value, and finally traverse the right subtree. This ensures all nodes are visited in sorted order for a BST. ✨ Example Input: [1, null, 2, 3] Output: [1, 3, 2] 🧠 Key Idea Inorder traversal naturally retrieves nodes in ascending order if the tree is a Binary Search Tree (BST). ⏱ Complexity TypeValueTimeO(N) — each node visited onceSpaceO(H) — recursion stack (H = height of tree) 📎 GitHub Link: https://lnkd.in/gBgJShhT #LeetCode #BinaryTree #InorderTraversal #Recursion #DSA #C++ #ProblemSolving #CodingChallenge #Day124
To view or add a comment, sign in
-
-
🚀 Day 112 of #LeetCode Challenge! Problem: Diameter of Binary Tree 💡 My Approach: The diameter of a binary tree is the longest path between any two nodes (may or may not pass through the root). The trick is to calculate the height of each subtree while keeping track of the maximum left + right path encountered. Use a helper function height() that: Recursively computes left and right subtree heights. Updates the global diameter as the maximum sum of left and right heights. ✨ Example: Input: [1,2,3,4,5] Output: 3 Explanation: The longest path is [4,2,1,3] or [5,2,1,3] ⏱ Time Complexity: O(N) — visit every node once 💾 Space Complexity: O(H) — recursive stack (H = height of tree) 🌱 Key Insight: At each node, the longest path through it is leftHeight + rightHeight. The overall diameter is the maximum of all such paths. 👨💻 GitHub Link: https://lnkd.in/dvUDMZUC #LeetCode #BinaryTree #Recursion #TreeTraversal #DSA #CodingChallenge #C++ #Day112
To view or add a comment, sign in
-
-
🔥 HyperRevision with Structure – Day 87 🔥 🧩 Problem solved today: 1️⃣ Surrounded Regions (LeetCode 130) 💭 Thoughts: Back to Neetcode 150. At first glance, this problem looks just like Number of Islands, but there’s a subtle twist — you need an extra boundary check before marking 'O's as 'X'. The key idea is to start DFS from the border 'O's so that only the inner, fully-surrounded regions are flipped. ⏱️ Time Complexity: O(m × n) 💾 Space Complexity: O(m × n) (due to recursion stack) 📌 GitHub link in comments #HyperRevision #LeetCode #DFS #GraphTraversal #NeetCode150
To view or add a comment, sign in
-
-
🚀 Day 117 of #LeetCode Challenge! Problem: Univalued Binary Tree 💡 My Approach: We need to check if all nodes in the binary tree have the same value. 🔍 Steps: Store the root value as the target value DFS through every node If any node's value ≠ root value → return false If traversal completes with no mismatch → return true ✅ 📌 Key Idea: A binary tree is univalued if every node contains the same value. ✨ Example: Input: [1,1,1,1,1,null,1] Output: true Input: [2,2,2,5,2] Output: false ⏱ Time Complexity: O(N) — every node is checked 💾 Space Complexity: O(H) — recursion stack (H = tree height) 👨💻 GitHub Link: https://lnkd.in/g3xFQpAm #LeetCode #BinaryTree #DFS #Recursion #DSA #Day117 #C++ #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge – Problem 81: Search in Rotated Sorted Array II Today I solved an interesting problem that tests both binary search optimization and edge case handling in rotated arrays. 🔍 Problem Summary: We’re given a rotated sorted array (which may contain duplicates) and need to determine whether a given target exists in it. 💡 Approach: Use a modified binary search to handle the rotation and duplicate values. If the middle element matches the target, return true. When duplicates are present (nums[low] == nums[mid] == nums[high]), adjust both ends to skip redundancy. Determine which side (left or right) is properly sorted and move the search boundaries accordingly. Continue until the target is found or the range is exhausted #LeetCode #DSA #CodingJourney #BinarySearch #ProblemSolving #CPlusPlus #StriversDSASheet ---
To view or add a comment, sign in
-
-
Day 57 of My DSA Challenge Problem: Given an array and a window size k, find the count of distinct elements in every window of size k. Example: Input: arr = [1, 2, 1, 3, 4, 2, 3], k = 4 Output: [3, 4, 4, 3] Approach Used: Used a HashMap to efficiently keep track of the frequency of elements inside the current window. Initially processed the first k elements, then slid the window one step at a time: Added the next element (right end) Removed the leftmost element Stored the size of the HashMap (distinct count) after every window slide. Time Complexity: O(N) Space Complexity: O(K) Takeaway: The combination of HashMap + Sliding Window makes it possible to track frequencies dynamically, helping solve problems that once seemed to require nested loops — in just linear time #Day57 #DSAChallenge #SlidingWindow #HashMap #ProblemSolving #CodingInJava #DataStructures #Algorithms #100DaysOfCode #GeeksforGeeks #LeetCode #CodeOptimization #CodingJourney #LearnDSA
To view or add a comment, sign in
-
-
🚀 Day 127 of #LeetCode Challenge! Problem: Binary Tree Level Order Traversal 💡 My Approach: This problem involves traversing the binary tree level by level — also known as Breadth-First Search (BFS) traversal. Here’s the step-by-step breakdown: Use a queue to store nodes level by level. For each level, store all node values in a temporary list. Push their left and right children into the queue. After finishing one level, add it to the result vector. ✨ Example Input: [3,9,20,null,null,15,7] Output: [[3], [9,20], [15,7]] 🧠 Key Idea This traversal captures the hierarchical structure of the tree perfectly — often used in problems involving level-based processing, like Zigzag traversal or average of levels. ⏱ Complexity TypeValueTimeO(N) — each node visited onceSpaceO(N) — for queue and result storage 📎 GitHub Link: https://lnkd.in/gntMrKbU #LeetCode #BinaryTree #BFS #LevelOrderTraversal #DSA #ProblemSolving #C++ #CodingChallenge #100DaysOfCode #Day127
To view or add a comment, sign in
-
-
Day 22 of #100DaysOfCode Today I solved the “Permutation in String” problem on LeetCode — a tricky one that really sharpens your understanding of sliding window and frequency mapping in strings. 🧩 Problem in short: Given two strings s1 and s2, the task is to check if any permutation of s1 exists as a substring in s2. At first glance, it feels like a brute-force problem — generate all permutations of s1 and check each in s2. But that’s computational suicide for longer strings. So the challenge was to find a smarter, optimized approach. ⚙️ Intuitive Approach: Instead of generating permutations, I focused on character frequencies. Maintain two frequency arrays — one for s1 and one for the current window of s2. Slide the window across s2, adding and removing characters as you move. Whenever both frequency arrays match, it means that substring of s2 is a permutation of s1. This approach reduces the complexity drastically and relies on pattern recognition through frequency matching, not brute force. Every day in this challenge is a reminder that optimization isn’t about doing less work — it’s about doing the right work. #LeetCode #C++ #ProblemSolving #DSA #CodingJourney #100DaysOfCode
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