🚀 Day 31 of #100DaysOfCode – Binary Tree Today I solved an interesting Binary Tree problem: 👉 Problem: Given the root of a binary tree, return the sum of every node’s tilt. 🔎 Concept Used: Postorder Traversal (DFS) Recursion Tree Subtree Sum Calculation 💡 Key Idea: For each node: Calculate sum of left subtree Calculate sum of right subtree Compute tilt = |leftSum - rightSum| Add tilt to a global variable By using Postorder Traversal (Left → Right → Root), we can compute subtree sums and tilt in a single traversal. 📈 Time Complexity: O(n) 📦 Space Complexity: O(h) (height of tree) This problem helped me strengthen my understanding of: ✔️ Tree recursion ✔️ Depth First Search ✔️ Writing clean recursive logic Consistency is key. One problem at a time. 💪 #Java #DataStructures #BinaryTree #DSA #CodingChallenge #LearningJourney #100DaysOfCode
Binary Tree Problem Solved with Postorder Traversal
More Relevant Posts
-
🚀 Day 20 of #100DaysOfCode 🌱 Topic: Binary Trees / Recursion ✅ Problem Solved: LeetCode 105 – Construct Binary Tree from Preorder and Inorder Traversal 🛠 Approach: Used preorder to identify the root node. Used inorder to split left and right subtrees. Stored inorder indices in HashMap for fast lookup. Recursively built left and right subtrees using calculated ranges. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) (HashMap) #100DaysOfCode #Day20 #DSA #BinaryTree #Recursion #Java #LeetCode #ProblemSolving #CodingJourney #Consistency #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 41/100 | #100DaysOfDSA 💻🚀 Today’s problem: Find the Duplicate Number Goal: Given an array with n + 1 numbers where each number is in the range [1, n], find the duplicate number without modifying the array and using constant extra space. Approach: Binary Search on Answer Idea: • The duplicate must lie in the range 1 → n • Count how many numbers are ≤ mid • If the count is greater than mid → duplicate is in the left half • Otherwise search the right half Key insight: Using the pigeonhole principle, if more numbers fall in a range than expected, a duplicate must exist there. Time Complexity: O(n log n) Space Complexity: O(1) Big takeaway: Binary search isn’t just for sorted arrays — it can also be applied on the range of possible answers. Day 41 — patterns getting clearer. 🔥 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Java #CodingJourney #InterviewPrep #ProblemSolving #TechCommunity
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟐𝟗 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on applying binary search in a rotated sorted array with duplicates. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Search in Rotated Sorted Array II 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used modified binary search • Calculated mid index in each iteration • Identified which half of the array was sorted • Narrowed the search range accordingly To handle duplicates: • If nums[left] == nums[mid] == nums[right], shrink the search space by moving both pointers. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Rotated arrays require identifying the sorted half • Duplicate values can hide the sorted structure • Shrinking the search space helps handle ambiguous cases • Binary search logic often needs small adjustments for edge cases 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Average Time: O(log n) • Worst Case Time: O(n) (due to duplicates) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is not just about dividing the array — it's about understanding the structure of the data. 29 days consistent. On to Day 30 🚀 #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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
-
-
🚀 100 Day Target – Day 13 Problem: Find the Duplicate Number Concept: Cyclic Sort | Index Mapping Today’s focus was on detecting duplicates without modifying constraints or using extra space unnecessarily. Instead of brute force or sorting, applied index placement logic to identify the duplicate efficiently. Key Learnings: ✔ Understanding constraints changes the approach ✔ In-place techniques improve space efficiency ✔ Strong array fundamentals simplify tricky problems From basics to optimized thinking — every day sharpening problem-solving instincts. The streak continues. 💪🔥 #100DaysOfCode #DSA #Java #Arrays #CyclicSort #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 40/100 | #100DaysOfDSA 💻🔥 Today’s problem: Subarray Sum Equals K Goal: Find the total number of subarrays whose sum equals k. Approach: Prefix Sum + HashMap Idea: • Keep track of the running sum while traversing the array • If (currentSum - k) was seen before, a subarray with sum k exists • Use a HashMap to store prefix sums and their frequencies Key insight: If prefixSum[j] - prefixSum[i] = k, then the subarray between them sums to k. Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Prefix sums combined with hashing can turn many subarray problems from O(n²) to O(n). Day 40 — still stacking patterns. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #PrefixSum #HashMap #Java #CodingJourney #InterviewPrep #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 37 of My DSA Journey — Cloning a Graph Today I solved the classic problem: Clone Graph. At first glance, it looks simple — just copy nodes and their neighbors. But the real challenge is handling cycles and avoiding infinite recursion. 🔍 Key Concept Learned: This problem is a perfect application of DFS with HashMap. The HashMap helps us: • Keep track of already cloned nodes • Prevent infinite loops in cyclic graphs • Maintain correct neighbor relationships 🧠 Approach I used: Use a HashMap to store original → cloned node mapping Traverse the graph using DFS Clone the node Recursively clone its neighbors Connect cloned neighbors to the cloned node ⏱️ Time Complexity: O(V + E) 📦 Space Complexity: O(V) 💡 Biggest takeaway: Whenever a problem involves copying structures with cycles (graphs, linked lists with random pointers), always think of using a HashMap to track visited nodes. This problem strengthened my understanding of: • Graph traversal • DFS recursion • Deep copy vs shallow copy • HashMap optimization Consistency is slowly turning confusion into clarity. One problem at a time. 💪 #Day37 #DSA #Java #GraphTheory #CodingJourney #LeetCode #SoftwareEngineering #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 43/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Search a 2D Matrix II Interesting matrix pattern here. Matrix properties: • Each row is sorted left → right • Each column is sorted top → bottom Key idea: Start from the top-right corner. Why? • If current value > target → move left • If current value < target → move down This way we eliminate one row or column every step. Time Complexity: O(m + n) Space Complexity: O(1) Big takeaway: Choosing the right starting point can simplify the entire search. Matrix patterns getting stronger day by day. 🔥 Day 43 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Matrix #BinarySearch #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day - 90 Binary Tree Maximum Path Sum The problem - Given the root of a binary tree, return the maximum path sum of any non-empty path. A path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. The path sum is the sum of the node values in the path. Brute Force - For each node, consider all possible paths starting from that node using DFS, calculate their sums, and track the maximum. This gives O(n²) time complexity as we explore multiple paths from each node with redundant calculations. Approach Used - •) Declare res = {root.val}. •) Call helper function: dfs(root , res). •) Return res[0]. Helper Function - dfs( Treenode node , int[] res) •) If node == null, return 0 (no contribution from null node). •) leftSum = Math.max(0, dfs(node.left, res)), taking max with 0 to ignore negative paths. •) rightSum = Math.max(0, dfs(node.right, res)), taking max with 0 to ignore negative paths. •) Update maximum sum, res[0] = Math.max(res[0], leftSum + rightSum + node.val), this considers path through current node connecting both subtrees. •) Return Math.max(leftSum, rightSum) + node.val, parent can use only one branch left or right. Complexity - Time - O(n), where n = number of nodes. Space - O(h), where h = height of tree. Note - At each node, we consider two scenarios: (1) the maximum path passing through this node (connecting both subtrees), which updates the global maximum, and (2) the maximum path extending from this node to its parent (using only one subtree), which is returned. We use Math.max(0, childSum) to ignore negative contributions, as excluding negative paths yields better sums. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
Day 19 of my Data Structures & Algorithms journey. Today’s problem: Same Tree (Binary Tree recursion) on LeetCode. At first glance it looks simple, but it teaches an important idea: Two trees are identical only if both their structure and node values match at every level. Key concept practiced today: Depth-First Search (DFS) with recursion. Step by step the logic becomes: • If both nodes are null → trees match • If one node is null → trees differ • If values differ → trees differ • Otherwise → recursively check left and right subtrees Small problems like these quietly sharpen problem-solving muscles. Consistency > intensity. #LeetCode #DSA #Java #BinaryTree #CodingJourney
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