Day 93 of #100DaysOfCode Today I solved "Add One Row to Tree" on LeetCode using a DFS approach. Key Idea: We need to insert a new row of nodes at a given depth, while preserving the existing structure below it. Approach: • If depth == 1: Create a new root and attach the original tree as its left child • Otherwise: Traverse the tree using DFS When we reach level depth - 1: Create new nodes with given value Attach original left subtree to new left node Attach original right subtree to new right node Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Tree modification Time Complexity: O(n) Space Complexity: O(h) This problem was a great exercise in modifying tree structure while maintaining connections correctly Not just traversing trees now… actually reshaping them #Day93 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
Solved Add One Row to Tree on LeetCode with DFS
More Relevant Posts
-
Day 72/150 🚀 LeetCode 104: Maximum Depth of Binary Tree 🧠 Problem Given the root of a binary tree, return its maximum depth. 📌 Example Input → root = [3,9,20,null,null,15,7] Output → 3 💡 Approach • Use recursion (DFS) • Calculate left subtree depth • Calculate right subtree depth • Return max(left, right) + 1 ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day72 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
66 of #100DaysOfCode 🚀 💡 Problem Solved: Find the Pivot Index (Prefix Sum Approach) Today I worked on a classic array problem where the goal is to find an index such that the sum of elements on the left is equal to the sum on the right. 🔍 Approach 1: Brute Force For every index, calculate left sum and right sum separately Time Complexity: O(n²) Simple but inefficient ⚡ Approach 2: Prefix Sum (Optimized) Compute total sum of array Traverse once while maintaining left sum Right sum = totalSum - leftSum - currentElement If leftSum == rightSum → pivot found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #DSA #LeetCode #CodingJourney #PrefixSum #ProblemSolving #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
Day 34/75 🚀 Solved Leaf-Similar Trees (LeetCode 872) today! ✅ All test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 15.37 MB 🔍 Approach: Used DFS (recursion) to collect leaf nodes of both trees. ✔️ Traversed each tree and stored leaf values in a vector ✔️ A node is a leaf if it has no left and right child ✔️ Compared both leaf sequences If both sequences match → trees are leaf-similar ✅ 💡 Key Learning: Breaking problems into collect + compare steps simplifies logic. DFS is very useful for extracting specific patterns from trees. Consistency + clarity = better problem breakdown 🌳✨ #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
Day 91 of #100DaysOfCode Today I solved "Insert into a Binary Search Tree" on LeetCode using a Recursive approach. Key Idea: In a Binary Search Tree (BST): • Left subtree → values less than root • Right subtree → values greater than root So we just follow the correct path until we find the right position to insert the new value. Approach: • If root is null → create and return new node • If value > root → go to right subtree • Else → go to left subtree • Recursively insert until correct position is found Concepts Used: • Binary Search Tree (BST) • Recursion • Tree traversal Time Complexity: O(h) Space Complexity: O(h) This problem reinforces the importance of BST properties for efficient insertion Simple logic, powerful structure #Day91 #100DaysOfCode #LeetCode #BST #Recursion #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 83/150 🚀 LeetCode 129: Sum Root to Leaf Numbers 🧠 Problem Each root-to-leaf path represents a number. Return the total sum of all root-to-leaf numbers. 💡 Approach • Use DFS traversal • Build number while moving down the tree (val * 10 + node) • At leaf node, return the formed number • Sum values from left and right subtree ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day83 #LeetCode #BinaryTree #DFS #Recursion #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 97 of #100DaysOfCode Today I solved "Maximum Width of Binary Tree" on LeetCode using Level Order Traversal (BFS) + Indexing. Key Idea: Treat the binary tree like a complete binary tree by assigning indices to nodes: • Left child → 2 * index • Right child → 2 * index + 1 This helps us calculate the width of each level easily. Approach: • Use a queue storing {node, index} • For each level: Take first and last index Width = right - left + 1 • Normalize indices to avoid overflow Concepts Used: • Binary Trees • Breadth First Search (BFS) • Queue • Index-based traversal Time Complexity: O(n) Space Complexity: O(n) This problem was interesting because it combines tree traversal with indexing logic to solve a non-trivial problem From simple traversal → to smart indexing tricks #Day97 #100DaysOfCode #LeetCode #BinaryTree #BFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 79/150 🚀 LeetCode 114: Flatten Binary Tree to Linked List 🧠 Problem Given the root of a binary tree, flatten it into a linked list in-place following preorder traversal. 📌 Example Input → root = [1,2,5,3,4,null,6] Output → [1,null,2,null,3,null,4,null,5,null,6] 💡 Approach • Use reverse preorder traversal • Flatten right subtree • Flatten left subtree • Connect nodes using previous pointer ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day79 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 245 of #500DaysOfCode Solved LeetCode 1559 – Detect Cycles in 2D Grid 🔄 💡 Approach: Traverse the grid using DFS Maintain a visited matrix While exploring neighbors: Move only in 4 directions (up, down, left, right) Only continue if the character matches Track parent cell (px, py) to avoid false cycle detection If we reach a visited cell that is not the parent → cycle found ✅ ⚡ Key Insight: Classic cycle detection in an undirected graph Parent tracking is crucial to avoid revisiting immediate previous node ⏱️ Complexity: Time: O(m × n) Space: O(m × n) (visited + recursion stack) ✨ Takeaway: Grid problems often map to graph traversal → think DFS/BFS + cycle detection patterns. Day 245 ✅ Still consistent 💪🔥 #LeetCode #DSA #DFS #Graphs #GridProblems #Consistency
To view or add a comment, sign in
-
-
Day 81/150 🚀 LeetCode 18: 4Sum 🧠 Problem Given an array of integers, return all unique quadruplets such that their sum equals the target. 📌 Example Input → nums = [1,0,-1,0,-2,2], target = 0 Output → [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] 💡 Approach • Sort the array • Fix first two elements • Use two pointers for remaining pair • Skip duplicates to avoid repeated quadruplets ⏱ Time Complexity O(n³) 📦 Space Complexity O(1) (excluding output) ✅ Result: Accepted ⚡ Runtime: 14 ms (Beats 92.37%) Hard problem handled — consistency continues strong 💪 #Day81 #LeetCode #DSA #TwoPointers #Arrays #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🔁 Solved Linked List Cycle using Floyd’s Cycle Detection Algorithm solved Problem #141 – Linked List Cycle on LeetCode with 97.68% runtime performance ⚡ 💡 Problem Summary Given the head of a linked list, determine whether the list contains a cycle (loop). A cycle exists if a node can be reached again by continuously following the next pointer. ⚙️ Approach – Floyd’s Tortoise & Hare Algorithm Used the two-pointer technique: 🐢 Slow pointer → moves 1 step at a time 🐇 Fast pointer → moves 2 steps at a time 👉 If a cycle exists, the fast pointer will eventually meet the slow pointer. 👉 If fast reaches NULL, there is no cycle. This avoids extra memory and is the most optimal solution. ⏱️ Complexity Analysis Time Complexity: O(n) → Each node is visited at most once. Space Complexity: O(1) → No extra data structures used (constant space). 🧠 Key Learning A great example of how pointer techniques can replace hashing and reduce space complexity from O(n) → O(1). More problems coming soon! #DSA #LinkedList #LeetCode #Cpp #ProblemSolving
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