🚀 LeetCode Practice: Problem #145 – Binary Tree Postorder Traversal I recently solved LeetCode 145, a classic tree traversal problem that strengthens understanding of Depth-First Search (DFS) and recursion. 📌 Problem Statement Given the root of a binary tree, return the postorder traversal of its nodes’ values. Postorder Traversal Order: 👉 Left → Right → Root 🧠 Technique Used: Depth-First Search (Recursive Approach) Approach Explanation: If the current node is None, return. Recursively traverse the left subtree. Recursively traverse the right subtree. Visit (append) the root node value at the end. This traversal ensures that child nodes are processed before their parent node. Time Complexity: O(n) Space Complexity: O(h) (Where h is the height of the tree due to recursion stack) #LeetCode #Python #DSA #ValidAnagram #ProblemSolving #Algorithms #DataStructures #CodingPractice #100DaysOfCode #InterviewPreparation #SoftwareEngineering #DeveloperJourney #TechCommunity #LearningByDoing
AMIT vishwakarma’s Post
More Relevant Posts
-
🚀 Day 10/30 | LeetCode Problem: Reverse Linked List (206) Problem: Given the head of a singly linked list, reverse the list and return the new head. 💡 Approach (Iterative – Three Pointers) To reverse a linked list, we carefully change the direction of pointers. We use three variables: prev → previous node current → current node next_node → temporarily stores next node Steps: Store next node Reverse the current node’s pointer Move prev and current one step forward Repeat until the list ends Finally, prev becomes the new head. ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🧠 Python Code class Solution: def reverseList(self, head): prev = None current = head while current: nxt = current.next current.next = prev prev = current current = nxt return prev 📌 Example Input: [1,2,3,4,5] Output: [5,4,3,2,1] 🎯 Key Takeaway Linked List problems are all about pointer manipulation. Understanding pointer flow is more important than memorizing code. ✅ Accepted 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day10 #Python #LinkedList #DataStructures #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 47/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 191 - Number of 1 Bits (Easy) 🧠 Approach 1: Using Brian Kernighan’s Algorithm. 💻 Solution: class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n: n &= (n - 1) count += 1 return count ⏱ Time | Space: O(k) | O(1) 📌 Key Takeaway: Using n & (n - 1) efficiently removes one set bit at a time, making it faster than checking every bit individually. 🧠 Approach 2: Python’s built-in binary conversion makes counting set bits simple 💻 Solution: class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1') #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 41 of #100DaysofCode Challenge! Today’s focus was on recursion and backtracking patterns: 🔹 Generate Parentheses Solved this using a backtracking approach by building valid combinations incrementally while maintaining two constraints: The number of opening brackets used must not exceed n The number of closing brackets must not exceed the number of opening brackets This ensures only valid sequences are generated without checking invalid ones afterward. Core idea: Use recursion to explore all valid states Add '(' if openings < n Add ')' if closings < openings Key takeaways: ✔ Backtracking with constraint pruning ✔ Recursive state-space exploration ✔ Understanding how valid combinations grow systematically Time Complexity: O(4ⁿ / √n) (related to Catalan numbers) Space Complexity: O(n) for recursion stack Building stronger intuition for recursion and combinatorial problems every day 🚀 📸 Screenshots of solution are attached! #Day41 #DynamicProgramming #BinaryTree #TreeDP #Algorithms #ProblemSolving #Python #Java #CodingMindset #SoftwareEngineering #AdityaVerma #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 169 of My LeetCode Journey 🚀 1092. Shortest Common Supersequence 🫧 In this problem, we are given two strings, and we need to build the shortest string that contains both of them as subsequences. ▪️ The key idea is related to the Longest Common Subsequence. If both strings share some common characters in order, we should include those characters only once in the final string. ▪️ First, I built a DP table to find the LCS length between the two strings. This helps us understand which characters are common between them. ▪️ After filling the DP table, I traversed it backwards to construct the final string. ▪️ If characters in both strings match, I add that character once to the result and move diagonally in the table. ▪️ If they don’t match, I move in the direction that gave the larger LCS value and add that character to the result. ▪️ If one string finishes before the other, I simply added the remaining characters from the other string. ▪️ In the end, I reversed the collected characters to get the shortest common supersequence, which contains both strings while keeping the length as small as possible. #LeetCode #DynamicProgramming #Strings #Python #CodingJourney #Day169 🔥
To view or add a comment, sign in
-
-
🚀 Day 18 of #100DaysOfCode 📌 LeetCode 977 – Squares of a Sorted Array Today’s problem was about transforming a sorted array (with possible negative numbers) into a new array of squared values that remains sorted. 💡 Approach Used: Two Pointers Instead of squaring all elements and sorting again (O(n log n)), I applied the Two Pointer technique to compare absolute values from both ends and filled the result array from the back. 🧠 Key Learnings: ✔️ Importance of optimal thinking over brute force ✔️ How negative numbers affect sorting after squaring ✔️ Efficient use of Two Pointers in array problems ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) 🔍 Problem Pattern: Array + Two Pointers Consistency is the key — showing up every day and improving step by step! 💪 #Day18 #100DaysOfCode #LeetCode #DSA #Python #CodingJourney #BTech #DataStructures
To view or add a comment, sign in
-
-
Day 43 of 365 days of code Qn 1) simplify path approach used: stack initialize a stack 1) iterate the string path using var i from 0 to len(path) 2) ignore forward slashes 3) initialize ch="" 4) while i<len(path) and path[i] not equal to forward slash: ch+=path[i];i+=1 5) if ch==.. stack.pop elif ch== . continue else: stack.push(ch) 6)i+=1 7) pop all the element of the stack and put them in another stack 8) initialize a string starting with/ as u iterate pop the stack and add the forward slash to the popped string. #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 47 of #180DaysOfCode Today I solved LeetCode 921 – Minimum Add to Make Parentheses Valid. This problem focuses on balancing parentheses by determining the minimum number of additions required to make the string valid. A great exercise for improving understanding of stacks and string traversal. 🔹 Problem: LeetCode 921 – Minimum Add to Make Parentheses Valid https://lnkd.in/dVdQdTYZ 📂 GitHub Repo: https://lnkd.in/dR7YYD_m #Python #DSA #LearnInPublic #CodingChallenge #LeetCode #DataStructures #Algorithms #PythonDSA #DailyCoding #Consistency #TechJourney #Upskill #CodeOfTheDay #Day47
To view or add a comment, sign in
-
✅ Day 74 of 100 Days LeetCode Challenge Problem: 🔹 #653 – Two Sum IV: Input is a BST 🔗 https://lnkd.in/g9vDFKMA Learning Journey: 🔹 Today’s problem required checking whether there exist two nodes in a Binary Search Tree whose values sum to k. 🔹 I performed a Depth-First Search (DFS) traversal using a stack to visit all nodes of the tree. 🔹 While traversing, I stored each node’s value in a list. 🔹 Then I used a hash set to track visited values and checked whether the complement (k - value) already exists. 🔹 If the complement is found, it means two numbers sum to k, so the function returns True. Concepts Used: 🔹 Depth-First Search (DFS) 🔹 Stack-based Tree Traversal 🔹 Hash Set for Fast Lookup 🔹 Two Sum Pattern Key Insight: 🔹 The classic Two Sum approach works well after collecting values from the BST. 🔹 Using a set allows O(1) lookup, making it efficient to check complements during iteration. Complexity: 🔹 Time: O(n) 🔹 Space: O(n) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
✅ Day 64 of 100 Days LeetCode Challenge Problem: 🔹 #1784 – Check if Binary String Has at Most One Segment of Ones 🔗 https://lnkd.in/gpJYed9J Learning Journey: 🔹 Today’s problem focused on determining whether a binary string contains only one contiguous segment of '1's. 🔹 Since the string has no leading zeros, the first character is always '1'. 🔹 If a '1' appears again after a '0', it indicates the start of a second segment. 🔹 A single pass through the string is enough to detect this pattern. Concepts Used: 🔹 String Traversal 🔹 Pattern Detection 🔹 Conditional Logic Key Insight: 🔹 The pattern "01" followed by another '1' reveals multiple segments of ones. 🔹 Detecting this transition allows us to solve the problem efficiently. Complexity: 🔹 Time: O(n) 🔹 Space: O(1) 💬 What’s your favorite trick for solving binary string problems? #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
✅ Day 69 of 100 Days LeetCode Challenge Problem: 🔹 #1009 – Complement of Base 10 Integer 🔗 https://lnkd.in/geVPugvi Learning Journey: 🔹 Today’s challenge involved finding the bitwise complement of a base-10 integer. 🔹 I first converted the integer into its binary representation using bin(n)[2:] to remove the 0b prefix. 🔹 Then I iterated through each bit and flipped it: • 0 becomes 1 • 1 becomes 0 🔹 After constructing the flipped binary string, I converted it back to a decimal integer using int(s, 2). Concepts Used: 🔹 Binary Representation 🔹 Bit Manipulation 🔹 String Traversal 🔹 Base Conversion (Binary → Decimal) Key Insight: 🔹 Converting the number to binary makes it easy to flip bits directly. 🔹 After inversion, converting the binary string back to base-10 produces the required complement. Complexity: 🔹 Time: O(b) where b is the number of bits in n 🔹 Space: O(b) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
Explore related topics
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