Leetcode potd : Sum of Root to leaf Binary Numbers Intuition : Since each node contains only 0 and 1, every root-to-leaf path naturally forms a binary number (with root as MSB). So idea is simple🌿, traverse all possible paths from root to leaf and keep building the number along the way instead of storing the full path. At every step, update the curr val so it represents the binary formed till that node. If reached the leaf node 🍃, that means one complete binary number is formed, so add to ans. So basically, it’s just dfs + building the number while moving down the tree. #leetcode #potd #problem_solving #consistency #consistency #programming
Sum of Root to Leaf Binary Numbers LeetCode Problem
More Relevant Posts
-
Leetcode POTD: Sort Integers by the Number of 1 Bits Today’s POTD was so easy 😄 if you know about built-in popcount and how to write a custom comparator. Intuition: LeetCode is focusing a lot on binary these days, so it was pretty intuitive to use built-in popcount 💡 Count set bits, sort based on that, and if bits are same -> sort in ascending order Attached my notes for reference ✍️ #leetcode #potd #consistency #programming #binary #bitManipulation
To view or add a comment, sign in
-
-
Leetcode POTD : Find Unique Binary Strings... Given an array that contains n unique binary strings, each of length n, and some possible strings are missing. Target: find a binary string of length n that is not present in the array. Thought process... Initially I thought of generating all possible binary strings of length n. Since the constraint was small, it passed all the test cases. But then I felt generating all 2^n strings just to find one missing is not really optimal, so I started thinking about a better approach 🤔 If we observe the examples carefully, we notice something interesting. There are n strings and each string has length n. Ohh yes, here is the trick 👀 Since the ansmust also be a string of length n, we can construct a new one ourselves. All the given strings are already unique, so if we flip the bit at index i from the i-th string (0 -> 1 or 1 -> 0), the newly formed string will differ from every string at least at one position. That guarantees the string will be unique and missing from the array ✨ Using this idea, the missing binary string can be found in O(n) time. #leetcode #potd #binary #strings #programming #problem_solving #consistency
To view or add a comment, sign in
-
-
DAY->12 Solved Running Sum of 1d Array on LeetCode today. Implemented the solution using the Prefix Sum technique to compute the cumulative sum of elements efficiently. -> Time Complexity: O(n) -> Space Complexity: O(n) All 54/54 test cases passed with 0 ms runtime. Small problems like this help strengthen fundamentals in arrays and prefix sums, which are widely used in many algorithmic problems. #LeetCode #DSA #CodingJourney #Programming #Cplusplus #ProblemSolving
To view or add a comment, sign in
-
-
Partitioning into minimum number of deci-binary numbers Deci-binary means a number that contains only 0s and 1s, and no leading zeroes. Thought process: 🤔 After looking at the sample inputs, I realized that for an n-digit number, we need at least as many deci-binary numbers as the highest digit present in it. For example, if the number is 25, the possible deci-binary options we can subtract are 11 or 10. We keep subtracting valid deci-binary numbers from the given number until it becomes zero, and at the same time, we increment the count ➕. Once the number becomes zero, we simply return the count ✅. #leetcode #problem_solving #programming #binary #potd #consistency
To view or add a comment, sign in
-
-
🚀 100 Days of LeetCode — Day 59 🧩 Problem Integer to Roman 💡 Core Idea To convert an integer into a Roman numeral, I used a place value mapping approach. The number is divided into four parts: Thousands Hundreds Tens Ones For each place value, I stored precomputed Roman numeral representations in arrays. Then I used direct indexing to select the correct Roman numeral and concatenate them. This approach avoids loops and complex conditions, making the solution simple and efficient. 📚 Key Learnings 1. Mapping values using arrays 2. Direct index lookup optimization 3. Breaking numbers using place value decomposition ⏱ Complexity Time Complexity: O(1) Space Complexity: O(1) #100DaysOfLeetCode #LeetCode #DSA #Programming #Cplusplus #CodingChallenge #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
k-th Lexicographical Happy String of Length n🪢 Given n and k, return the k-th lexicographical happy string of length n. Thought process Brute force idea: Generate all possible happy strings of length n, Sort them, Return the string at k-1 index Optimization Instead of generating everything and sorting later, we can control the order while generating. During backtracking: Try characters in order → a, b, c Skip if the current character is the same as the previous one This way strings are already generated in lexicographical order, so we can directly return the k-th string. #leetcode #thoughtProcess #problemSolving #programming #recursion #backtracking
To view or add a comment, sign in
-
-
Duplicate enum values can hijack your reverse lookup. Two members are assigned the same numeric constant. A console.log reads the enum by that number. In production this can surface as wrong status codes. Logs may point to an unexpected enum name. Buggy conditionals silently pick the last defined key. Comment A, B, C, or D with the output you think appears and defend your choice. #typescript #programming #codinginterview #backend
To view or add a comment, sign in
-
-
#Day56 solved LeetCode 169 Majority Element Problem: Find the element that appears more than ⌊n/2⌋ times in an array. Approach: Instead of using extra space like HashMap, I learned a very efficient method called the Boyer-Moore Voting Algorithm. The idea is simple: 1.Keep a candidate and a count 2.Increase count if same element appears 3.Decrease count if different element appears 4.The final candidate will be the majority element Time Complexity: O(n) Space Complexity: O(1) This problem taught me how powerful optimized logic can be compared to brute force solutions. #DSA #LeetCode #Coding #ProblemSolving #100DaysOfCode #Programmer #LearningJourney
To view or add a comment, sign in
-
-
LeetCode Parctice: Problem no.217 (Contains Duplicate) Statement- A array is given , we have to return true if any value appears at least twice in the array, and return false if every element is distinct. Approach- Brute Force Key Idea: - I used sets data structure for this problem as set only store unique elements. -While traversing the array, I check whether the current element already exists in the set. If the element is already present → it means a duplicate exists, so return true. Otherwise, insert the element into the set and continue checking. Time Complexity = O(n) Space Complexity = O(n) #DSA #CodingPractice #Cpp #Programming #ProblemSolving
To view or add a comment, sign in
-
-
Minimum Changes to Make Alternating Binary String Intuition A binary string can be alternating in only two ways: 0101... or 1010.... Check one pattern while iterating through the string. If a character doesn’t match the expected bit, we increase a counter cnt (number of changes needed). But this pattern might not be optimal. The operations needed for the other pattern will be: len(s) - cnt So we simply return: min(cnt, len(s) - cnt) This gives the minimum number of changes required to make the string alternating. #problem_solving #leetcode #programming
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