Day 14 Today, I continued my DSA practice on LeetCode and solved a design-based problem. I solved the "Implement Stack using Queues" problem using C++, and my solution passed all test cases. This problem helped me practice: • Understanding Stack vs Queue (LIFO vs FIFO) • Using queues to simulate stack behavior • Designing efficient data structures Problem: Implement a Last-In-First-Out (LIFO) stack using only two queues. The implemented stack should support all standard operations: • push(x) → Push element onto stack • pop() → Remove the top element • top() → Get the top element • empty() → Check if the stack is empty You must use only standard queue operations. Example: Input: ["MyStack","push","push","top","pop","empty"] [[],[1],[2],[],[],[]] Output: [null,null,null,2,2,false] Explanation: Stack follows LIFO, so the last pushed element is removed first. #Coding #CPP #DSA #Stack #Queue #ProblemSolving #LearningJourney #LeetCode #Consistency #ChandigarhUniversity #GDG
Implement Stack using Queues in C++
More Relevant Posts
-
Day 51 of 100 Days of Coding #100Daysofcoding #coding#dsa 1.Count Good Nodes in Binary Tree Define function dfs(node, maxVal) If node is NULL → return 0 Check if current node is a good node: If node.val >= maxVal → count = 1 Else → count = 0 Update max value: maxVal = max(maxVal, node.val) Recurse: Left → dfs(node.left, maxVal) Right → dfs(node.right, maxVal) Return: count + left + right Code: class Solution: def goodNodes(self, root: TreeNode) -> int: def dfs(root,maxval): if not root: return 0 res=1 if root.val>=maxval else 0 maxval=max(maxval,root.val) res+=dfs(root.left,maxval) res+=dfs(root.right,maxval) return res return dfs(root,root.val) Time Complexity-O(N) Space Complexity-O(H)
To view or add a comment, sign in
-
-
Day 52 of 100 Days of Coding #100Daysofcoding #coding#dsa 1.Path Sum Define function dfs(node, currSum) If node is NULL → return False Add current node value: currSum += node.val Check if it’s a leaf node (no left & no right): If currSum == targetSum → return True Else → return False Recurse: Left → dfs(node.left, currSum) Right → dfs(node.right, currSum) Return: left result OR right result Code- class Solution: def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: def dfsum(root,cursum): if not root: return False cursum+=root.val if not root.left and not root.right: return cursum==targetSum return (dfsum(root.left,cursum) or dfsum(root.right,cursum)) return dfsum(root,0) Time Complexity-O(N) Space Complexity-O(H)
To view or add a comment, sign in
-
-
Day 160/365 – DSA Challenge 🚀 Solved Same Tree on LeetCode today. 🔹 Problem: Given the roots of two binary trees p and q, check whether they are identical (same structure and same node values). 🔹 Approach Used: Depth First Search (DFS) / Recursion Steps followed: 1️⃣ If both nodes are NULL, return true. 2️⃣ If one is NULL and the other isn’t, return false. 3️⃣ Check if current node values are equal. 4️⃣ Recursively compare: Left subtree of both trees Right subtree of both trees 🔹 Key Idea: Two trees are the same only if every corresponding node matches in value and position. 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(h) (recursion stack) 💻 Language: C++ This problem strengthens understanding of tree comparison and recursive traversal, which is a common pattern in tree-based problems. #Day160 #365DaysOfCode #DSA #LeetCode #BinaryTree #DFS #Recursion #CodingChallenge #Cpp
To view or add a comment, sign in
-
-
🚀 Cracking Binary Tree Problems with Optimal Efficiency! I’m excited to share that I successfully solved the “Binary Tree Right Side View” problem on LeetCode using an optimized approach, achieving 0 ms runtime (beating 100% of C++ submissions) ⚡ 🔍 Problem Statement Given a binary tree, the task is to return the values of nodes visible when the tree is viewed from the right side, listed from top to bottom. 💡 My Approach Instead of using the commonly preferred BFS (Level Order Traversal), I implemented a Recursive DFS approach with optimization: ✔ Key Strategy: Right-first traversal (Root → Right → Left) ensures that rightmost nodes are prioritized. Level tracking technique is used to capture only the first node encountered at each depth. At each level, if it is the first visit, that node is added to the result vector. ⚙️ Complexity Analysis Time Complexity: O(N), where N is the number of nodes Space Complexity: O(H), where H is the height of the tree (recursion stack) 📊 Performance Results ⏱ Runtime: 0 ms (Beats 100% of C++ submissions) 💾 Memory: 14.86 MB (Beats 85.22% of submissions) 🧠 Key Takeaway Sometimes, a simple shift in traversal order combined with smart level tracking can replace complex logic and still deliver optimal performance. Consistency in solving DSA problems continues to sharpen my problem-solving intuition and coding efficiency. #LeetCode #DSA #Cplusplus #BinaryTree #Algorithms #ProblemSolving #CodingJourney #TechCommunity #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 32/75 🚀 Solved Maximum Depth of Binary Tree (LeetCode 104) today! ✅ All 39/39 test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 18.94 MB (Beats ~77%) 🔍 Approach: Used recursion (DFS) to calculate depth of the tree. ✔️ If node is NULL → return 0 ✔️ Recursively find depth of left subtree ✔️ Recursively find depth of right subtree ✔️ Return 1 + max(leftDepth, rightDepth) This naturally explores the tree depth-first. 💡 Key Learning: Tree problems often become intuitive with recursion. Think in terms of subproblems (left + right) and combine results. Consistency + recursion thinking = strong tree concepts 🌳 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
Day 35/75 🚀 Solved Count Good Nodes in Binary Tree (LeetCode 1448) today! ✅ All 63/63 test cases passed ⚡ Runtime: 91 ms (Beats ~82%) 💾 Memory: 88.34 MB (Beats ~64%) 🔍 Approach: Used DFS (recursion) while tracking the maximum value seen so far on the path. ✔️ Start from root with initial max = root value ✔️ If current node value ≥ max so far → it's a “good node” ✔️ Update max and continue traversal ✔️ Recursively check left and right subtree 💡 Key Learning: Passing state (like max value) in recursion helps solve path-based problems efficiently. No need to store full paths—just keep track of what's important. Consistency + depth thinking = stronger tree skills 🌳🔥 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
Infrastructure tooling is converging on the same idea: make the runtime a first-class compile target. "wterm" by Vercel compiles to WASM and renders terminals directly to the DOM with full accessibility. "weft" by WeaveMind treats LLMs and APIs as language primitives with type checking. "xata" by Xata.io turns Postgres instances into Copy-on-Write branches that scale to zero. The pattern holds across terminals, databases, and programming languages - the environment is becoming part of the type system. Bonus project: "dockerfile-roast" by Immanuel Tikhonov brings 63 opinionated linting rules into the build process with feedback that doesn't hedge. Links in the first comment!
To view or add a comment, sign in
-
Day 42 of 100 Days of Coding #100Daysofcoding #coding #dsa 1.Maximum Depth of Binary Tree DFS (Recursive) If node is empty → return 0 Find depth of left subtree Find depth of right subtree Take max of both Add 1 (for current node) Return it Dfs method CODE : class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 return 1+max(self.maxDepth(root.left),self.maxDepth(root.right)) BFS (Level Order – Queue) If root is empty → return 0 Put root in queue Set level = 0 While queue is not empty: Process all nodes in current level Add their children to queue Increase level Return level CODE: class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 level=0 q=deque([root]) while q: for i in range(len(q)): node=q.popleft() if node.left: q.append(node.left) if node.right: q.append(node.right) level+=1 return level Time Complexity- DFS → visit all nodes → O(n) BFS → visit all nodes → O(n) Space Complexity- DFS- O(h)Recursive stack BFS - O(n) Queue stores nodes level-wise
To view or add a comment, sign in
-
-
Day 162/365 – DSA Challenge 🚀 Solved Binary Tree Level Order Traversal on LeetCode today. 🔹 Problem: Given the root of a binary tree, return its level order traversal (i.e., level by level from left to right). 🔹 Approach Used: Breadth First Search (BFS) Steps followed: 1️⃣ Use a queue to process nodes level by level. 2️⃣ Push the root node into the queue. 3️⃣ For each level: Get the current queue size Process all nodes of that level Add their left and right children to the queue 4️⃣ Store each level’s values separately. 🔹 Key Idea: BFS naturally processes nodes in level-wise order, making it perfect for this problem. 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(n) 💻 Language: C++ This problem is a fundamental example of tree traversal using queues, widely used in many tree and graph problems. #Day162 #365DaysOfCode #DSA #LeetCode #BinaryTree #BFS #CodingChallenge #Cpp
To view or add a comment, sign in
-
-
One of the most powerful applications of LLMs is legacy code transformation. In this blog, I break down the key approaches to LLM-driven code migration—and what actually works in practice. https://lnkd.in/gs9rZcus
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