🚀 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
100 Days of Code: Same Tree Solution on LeetCode
More Relevant Posts
-
🚀 Day 70 of #100DaysOfCode Solved 107. Binary Tree Level Order Traversal II on LeetCode 🔗 🧠 Key Insight: This is just level order traversal (BFS) but: 👉 Return levels from bottom to top instead of top to bottom ⚙️ Approach (DFS with Level Tracking): 1️⃣ Start traversal with level = 0 2️⃣ For each node: 🔹 If level not present → insert new list at front 🔹 Add value at correct position: 👉 res.get(res.size() - 1 - level) 3️⃣ Recurse: 🔹 Left → level + 1 🔹 Right → level + 1 ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 82 – Univalued Binary Tree Solved a problem to determine whether all nodes in a binary tree have the same value using depth-first traversal. Key Learnings: Applied DFS recursion to traverse all nodes in the tree Compared each node’s value with the root value for validation #DSA #Java #BinaryTree #DFS #Recursion #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
🚀 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
To view or add a comment, sign in
-
-
🚀Day 40 LeetCode 1161 – Maximum Level Sum of a Binary Tree Ever wondered how to find the level of a binary tree with the maximum sum of node values? 🤔 Here’s the idea: We traverse the tree level by level (BFS) and compute the sum at each level. The trick is to keep track of the maximum sum and return the smallest level where this occurs. 💡 Key Insight: Level-order traversal (using a queue) naturally processes nodes one level at a time, making it perfect for this problem. 🔧 Approach: ✔ Use a queue for BFS ✔ For each level: • Calculate sum of nodes ✔ Track maximum sum and corresponding level 🔥 Takeaway: Whenever a problem involves levels in a tree, think BFS first — it often leads to the cleanest solution. #LeetCode #DataStructures #Java #CodingInterview #BinaryTree #BFS #ProblemSolving
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
-
-
Java Arrays: The Ultimate Building Blocks Before building complex data structures, the foundation needs to be rock solid. Arrays are the ultimate building blocks. Today, I locked down the 4 core operations: 1. Declare: Reserving contiguous memory. 2. Initialize: Populating the data. 3. Access: The magic of O(1). 4. Traverse: Looping through the elements. The biggest takeaway? Understanding fixed memory allocation in Java is crucial before moving to dynamic structures like ArrayLists or HashMaps. #DSA #Java #ArrayBasics
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
-
-
🧠 Day 36/75: Recursion within Recursion! Today’s challenge was "Subtree of Another Tree," a problem that perfectly illustrates how we can reuse fundamental patterns to solve more complex structures. The Strategy: To solve this, I combined two recursive ideas: Traverse the Main Tree: A standard DFS to visit every node in the primary tree. Structural Comparison: At every node, I used a helper function (isSameTree) to check if the current subtree perfectly matches the target structure. Performance: ✅ Runtime: 3 ms (Beats 67.99% of Java users) ✅ Complexity: $O(M \times N)$ in the worst case, but highly efficient for most tree structures. It’s incredibly satisfying to see yesterday's "Same Tree" logic become just one small part of today's larger solution. Build, repeat, and scale! 🚀 #LeetCode #75DaysOfCode #Java #BinaryTree #Recursion #DSA #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
Day 78 - Binary Tree Level Order Traversal Implemented level-by-level traversal of a binary tree using a queue (BFS). Approach: • Use a queue to process nodes • Traverse level by level • Track size of each level • Store values accordingly Time Complexity: O(n) Space Complexity: O(n) #Day78 #LeetCode #BinaryTree #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 62 of #100DaysOfCode 🌱 Topic: Trees / Recursion ✅ Problem Solved: LeetCode 112 – Path Sum 🛠 Approach: Used DFS (recursion) to explore all root-to-leaf paths. Base Case: If node is null → return false If leaf node: Check if target == node.val → return result Otherwise: Reduce target → target - node.val Recurse on left and right subtree #100DaysOfCode #Day62 #DSA #Trees #Recursion #DFS #LeetCode #Java #BinaryTree #ProblemSolving #CodingJourney #Consistency
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