🌳 Day 59/100: Preorder Traversal - Root, Left, Right Day 59. Learning tree traversals. Preorder: Visit root first, then explore. 🎯 📌 Problem: Return preorder traversal of binary tree. 🎯 Recursion: Base case: If node is null, return. Recursive case: Add value, go left, go right. Simple and clean. 📊 Complexity: O(n) time, O(h) space (recursion stack) 🌱 Tree Traversals: Preorder: Root → Left → Right Inorder: Left → Root → Right Postorder: Left → Right → Root Day 59. Traversal patterns building. 🌳 #100DaysOfCode #DSA #LeetCode #Day59 #Java #PreorderTraversal #BinaryTree #TreeTraversal
Urjit Upadhyay’s Post
More Relevant Posts
-
🚀 Day 34 of #100DaysOfCode Solved 852. Peak Index in a Mountain Array on LeetCode ⛰️📈 🧠 Key Insight: A mountain array strictly increases to a peak and then decreases. Instead of scanning the whole array, we can use Binary Search to efficiently find the peak. ⚙️ Approach: 🔹Use binary search with two pointers left and right 🔹Compare arr[mid] with arr[mid + 1] 🔹If arr[mid] > arr[mid + 1] → peak is on the left side (including mid) 🔹Otherwise → peak is on the right side 🔹Continue until left == right, which gives the peak index ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 40 — Maximum Depth of Binary Tree Solved LeetCode 104: Maximum Depth of Binary Tree. Used recursion: Base case → return 0 if node is null Recursively compute left and right depth Return 1 + max(left, right) Simple problem, but important for understanding tree height, recursion flow, and divide-and-conquer thinking. Building strong tree foundations step by step. #Day40 #LeetCode #BinaryTree #Recursion #DSA #Java #Consistency
To view or add a comment, sign in
-
-
Day 10/30 – LeetCode streak Today’s problem: Sum of Root To Leaf Binary Numbers. Each root-to-leaf path in the tree forms a binary number (top is MSB, leaf is LSB), and you need the sum of all such numbers. The pattern is a clean DFS with a running value: -As you go down the tree, carry a 'current' integer that represents the binary number so far. -At each node, shift left and add the node’s bit: 'current = (current << 1) | node.val. -When you hit a leaf ('left == null' and 'right == null'), return 'current' because that path is now a complete binary number. -For internal nodes, return 'dfs(left, current) + dfs(right, current)' so the sums bubble up. Day 10 takeaway: This is a nice example of “carry state down the recursion instead of storing the whole path” — the bit shift builds the number on the fly, and the recursion naturally adds up all path values. #leetcode #dsa #java #binarytree #consistency
To view or add a comment, sign in
-
-
🚀 Day 28 of #100DaysOfCode Solved 148. Sort List on LeetCode 🔗📊 🧠 Why this problem is interesting: Arrays are easy to sort, but linked lists don’t allow random access. That’s why Merge Sort becomes the perfect choice here. ⚙️ Approach (Merge Sort on Linked List): 🔹Use slow & fast pointers to find the middle 🔹Split the list into two halves 🔹Recursively sort both halves 🔹Merge two sorted linked lists ⏱️ Time Complexity: O(n log n) 📦 Space Complexity: O(log n) (recursive stack) #100DaysOfCode #LeetCode #DSA #LinkedList #MergeSort #Java #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀 DSA Consistency - Day 57 Today I solved the Same Tree problem on LeetCode, which focuses on understanding binary tree structure comparison using recursion. The goal is to determine whether two binary trees are structurally identical and have the same node values. 🧠 Approach: Recursive Tree Traversal To check if two trees are the same: 1️⃣ If both nodes are null, they are identical → return true. 2️⃣ If one node is null and the other is not, trees differ → return false. 3️⃣ If node values are different, trees are not identical. 4️⃣ Recursively check: Left subtree of both trees Right subtree of both trees Both must match for the trees to be identical. ⏱ Complexity Analysis Time Complexity: O(n) Each node is visited once. Space Complexity: O(h) Due to recursion stack (where h is the height of the tree). #DSA #LeetCode #BinaryTree #Java #CodingJourney #Consistency #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
#day333 of #1001daysofcode problem statement (0257): Binary Tree Paths 💡 Approach: Used DFS recursion to explore all root-to-leaf paths in the binary tree. While traversing, I kept building the path string. When a leaf node is reached, the complete path is added to the result list. Example path format: 1->2->5 ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of the tree) Consistency in solving one problem every day 📈 #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
🚀 Day 41 of #100DaysOfCode Solved 189. Rotate Array on LeetCode 🔄 🧠 Key Insight: Rotating an array by k steps to the right means the last k elements move to the front, while the remaining elements shift to the right. ⚙️ Approach: 1️⃣ Compute effective rotations using k % n 2️⃣ Store the last k elements in a temporary array 3️⃣ Shift the remaining n-k elements to the right 4️⃣ Copy the stored elements back to the beginning of the array This ensures the rotation happens in-place with controlled extra space. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(k) #100DaysOfCode #LeetCode #DSA #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 60/100 – LeetCode Challenge ✅ Problem: #67 Add Binary Difficulty: Easy Language: Java Approach: Reverse Iteration with Carry Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) Key Insight: Binary addition follows same rules as decimal: Sum bits + carry Result bit = sum % 2 New carry = sum / 2 Solution Brief: Iterated from rightmost bits of both strings. Tracked carry and built result from right to left using StringBuilder. Reversed final string for correct order. #LeetCode #Day60 #100DaysOfCode #Binary #Java #Algorithm #CodingChallenge #ProblemSolving #AddBinary #EasyProblem #StringManipulation #BitManipulation #DSA
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 36 📌 LeetCode – Path Sum Today I solved the Path Sum problem using a recursive Depth-First Search (DFS) approach. Given a binary tree and a target sum, determine whether there exists a root-to-leaf path such that the sum of all node values equals the target. 💡 Approach: ✔ If the node is null, return false. ✔ If it's a leaf node, check whether the remaining sum equals the node value. ✔ Recursively subtract the current node value from targetSum. ✔ Check both left and right subtrees. ✔ If either returns true → path exists. ⏱ Complexity: Time Complexity: O(n) Space Complexity: O(h) — height of the tree Binary tree problems are all about mastering recursion and understanding base cases clearly. #Java #LeetCode #DSA #BinaryTree #Recursion #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