📘 LeetCode Daily — Balanced Binary Tree Checked tree balance by recursively comparing left and right subtree heights at every node. The solution worked, but it clearly showed how repeated height calculations increase time complexity—an important reminder that correct logic isn’t always optimal logic. Solid recursion practice and a good lead-in to optimization. #LeetCode #BinaryTree #Recursion #DSA #Java
Balanced Binary Tree LeetCode Solution
More Relevant Posts
-
📘 LeetCode Daily — Balance a Binary Search Tree Solved this by first using inorder traversal to convert the BST into a sorted list, then reconstructing the tree from the middle element to ensure balance. This approach clearly showed how traversal order captures structure and how choosing the midpoint at each step naturally leads to a height-balanced tree. A neat example of combining traversal + divide-and-conquer. #LeetCode #BinarySearchTree #InorderTraversal #DivideAndConquer #DSA #Java
To view or add a comment, sign in
-
-
Solved "Container With Most Water" on LeetCode today using the Two-Pointer Technique. 🚀 The key insight is that the area formed by two lines depends on the shorter height and the distance between them. Starting with pointers at both ends of the array, we compute the area and move the pointer at the smaller height inward to potentially find a taller boundary and maximize the area. This approach efficiently reduces the problem from O(n²) brute force to O(n) time with O(1) space. Problems like this are a great reminder that the right observation can drastically optimize a solution. 💡 #LeetCode #DSA #TwoPointers #Java #ProblemSolving
To view or add a comment, sign in
-
-
Leetcode Problem | | Check If a String Contains All Binary Codes (1461) 🚀 Problem: Given a binary string s and integer k, check if all possible binary codes of length k exist in the string. Example: s = "00110", k = 2 Expected Output = true In substring and sliding window problems, boundary conditions matter more than logic sometimes. Consistent debugging > frustration. #DSA #Java #LeetCode #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Optimizing Sorting Logic Using Bit Manipulation (Java) Today I solved a problem that required sorting integers based on the number of set bits in their binary representation. Instead of using a direct sorting approach, I implemented a custom comparator with a PriorityQueue and used Brian Kernighan’s algorithm to count set bits efficiently: n = n & (n - 1) This reduces time complexity for counting bits to O(number of set bits). 📊 Result: • 77/77 test cases passed • Runtime: 7ms • Beat 78% of submissions Key takeaway: Optimization is not just about solving — it’s about choosing the right approach and reducing unnecessary operations. Bit manipulation continues to be one of the most powerful tools in algorithm design. #DSA #Java #BitManipulation #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfLeetCode Today I solved "Container With Most Water" problem on LeetCode. 🔹 Difficulty: Medium 🔹 Concept Used: Two Pointer Technique 🔹 Language: Java 📌 Problem Summary: Given an array representing heights of vertical lines, the goal is to find two lines that together with the x-axis can contain the maximum amount of water. 💡 Approach: Instead of checking all possible pairs (O(n²)), I used the Two Pointer approach which reduces the time complexity to O(n). Steps: 1️⃣ Start with two pointers at the beginning and end of the array. 2️⃣ Calculate the area using the smaller height. 3️⃣ Move the pointer with the smaller height inward. 4️⃣ Track the maximum area during each step. 📊 Result: ✅ Runtime: 5 ms (Beats 81.86%) ✅ Memory: Beats 95.54% This problem was a great exercise to understand optimization using two pointers instead of brute force. Consistency is the key — moving forward one problem at a time! 💪 #LeetCode #100DaysOfCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟑𝟐/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐂𝐨𝐮𝐧𝐭 𝐁𝐢𝐧𝐚𝐫𝐲 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠𝐬 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. Today’s challenge focused on grouping logic and pattern counting in strings. 💪 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Single-pass grouping technique that tracks the lengths of consecutive character blocks. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝟏) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: By comparing the sizes of adjacent character groups, we can efficiently count valid binary substrings without generating all substrings. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Strings #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🚀 Day 76 of #100DaysOfCode Today I solved a string manipulation problem: Clear Digits 🧠 Problem: Given a string, whenever a digit appears, remove the previous character. Digits act like a backspace operation. 💡 Key Insight: Instead of modifying the string directly (since Strings are immutable in Java), I used a StringBuilder, which works like a stack: If character → append (push) If digit → delete last character (pop) 📌 What I Learned: StringBuilder is powerful for string modifications Always handle edge cases (like digit at the start) Many string problems are actually stack problems in disguise ⏱ Complexity: Time: O(n) Space: O(n) #Java #DSA #LeetCode #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 18 of #100DaysOfCode Solved Remove Duplicates from Sorted List II on LeetCode 🔗 🧠 Key insight: When duplicates appear in a sorted linked list, we need to remove all occurrences, not just one. Using a dummy (sentinel) node makes it easier to handle cases where duplicates start at the head. ⚙️ Approach: 🔹Create a dummy node pointing to the head 🔹Traverse the list with two pointers 🔹If a duplicate sequence is found, skip the entire block 🔹Otherwise, move forward normally ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day: 43/365 📌 LeetCode POTD: Binary Number with Alternating Bits Easy Key takeaways/Learnings from this problem: 1. This one shows how clean bit tricks can replace messy string checks when working with binary patterns. 2. Big takeaway: sometimes a small observation (like using XOR or shifting) turns a pattern problem into a super neat one-liner. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
-
🚀 Day 18/180 | #180DaysOfCode 📍 LeetCode | 💻 Java Solved: 1528. Shuffle String Used a direct indexing approach by placing each character at its correct position using the given indices array. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) A simple yet important problem to strengthen understanding of arrays and index mapping. 💪 Consistency continues 🚀 #DSA #LeetCode #Java #CodingJourney #Consistency
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