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
Sum of Root to Leaf Binary Numbers LeetCode Solution
More Relevant Posts
-
🚀 Day 54 / 100 – LeetCode Challenge ✅ Solved: Same Tree (Easy) Today’s problem was about comparing two binary trees and checking whether they are structurally identical with the same node values. 🔍 Key Idea: Used Recursion to traverse both trees simultaneously: If both nodes are null → same If one is null → not same If values match → recursively check left & right subtrees 💡 This problem strengthened my understanding of: Tree Traversal Recursion Base Case Handling ⚡ Performance: ⏱ Runtime: 0 ms (Beats 100%) 💾 Memory: 43.07 MB Consistency is the key - one problem closer to the goal! 💪 #Day54 #LeetCode #DSA #BinaryTree #Java #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Today I solved LeetCode 572 – Subtree of Another Tree. 🧩 Problem Summary: Given the roots of two binary trees root and subRoot, check whether subRoot is a subtree of root. A subtree is a node in root along with all its descendants that forms a tree identical to subRoot. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree comparison 🧠 Approach: Traverse the main tree (root) using recursion. At each node: Check if the subtree starting from that node is identical to subRoot. Use a helper function to compare two trees: If both nodes are null → true If one is null → false If values differ → false Recursively compare left and right subtrees If a match is found at any node, return true. Otherwise, continue checking in left and right subtrees. 📚 What I Learned: Combining tree traversal with tree comparison. Reusing logic (Same Tree concept) effectively. Breaking complex problems into smaller reusable parts. Strengthening DFS and recursive problem-solving. Building strong foundations in tree problems step by step Consistency is the real game changer #LeetCode #DSA #BinaryTree #Subtree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Today I solved LeetCode 101 – Symmetric Tree. 🧩 Problem Summary: Given the root of a binary tree, check whether it is symmetric (mirror of itself). A tree is symmetric if the left subtree is a mirror reflection of the right subtree. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree comparison 🧠 Approach: Define a helper function to compare two nodes. Check if: Both nodes are null → symmetric One is null → not symmetric Values are equal → continue checking Recursively compare: Left subtree of left node with right subtree of right node Right subtree of left node with left subtree of right node If all checks pass, the tree is symmetric. This ensures a mirror comparison of the tree structure. 📚 What I Learned: How to compare two trees using recursion. Understanding mirror symmetry in binary trees. Strengthening DFS and recursive thinking. Breaking problems into smaller subproblems. Improving problem-solving skills one step at a time 🌳 Consistency is the key to growth 🚀 #LeetCode #DSA #BinaryTree #SymmetricTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge Problem: Palindrome Number Today’s problem focused on number manipulation without converting the integer into a string. Approach: If number is negative → return false Reverse the digits using modulo (% 10) and division (/ 10) Compare reversed number with original number Logic Used: Extract last digit: rem = x % 10 Build reversed number: rev = rev * 10 + rem Remove last digit: x = x / 10 Complexity: Time: O(log₁₀ n) Space: O(1) Key takeaway: Problems involving digits can often be solved mathematically without string conversion. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🧠 Day 27 / 100 – DSA Practice Solved Same Tree problem on LeetCode 🌳✅ 🔹 Problem Insight: Check whether two binary trees are structurally identical and have the same node values. 🔹 Approach: Used Breadth-First Search (BFS) with a queue to traverse both trees simultaneously. Compared nodes at each step: ✔️ If both are null → continue ❌ If one is null or values differ → return false 🔹 Complexity: Time → O(n) Space → O(n) 💯 Result: ✔️ All test cases passed ⚡ Runtime: 0 ms (Beats 100%) Exploring trees step by step 🌲🚀 #Day27 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BFS #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Finally day 6 of #60daysOfLeetcode! Life employed some dirty tricks to delay this one but here we are. Todays question. 3sum - https://lnkd.in/d8E-SVvx Solution. We sort the array initially in order to allow use of two pointers from both ends. Then we iterate using from the beginning of the array. For each position, we check for the other the two numbers that might add up to zero using opposing two pointers, i+1 and len(arr) - 1. If the sum is too large then the right pointer reduces, otherwise the left increases. If the sum equals to zero, that combination is saved. At end we return a list of these combinations. Time complexity is O(N^2) due to the nested loop. Space complexity is O(N) due to the set. #Java #LearningInPublic #LeetCode #DSA
To view or add a comment, sign in
-
-
Merge Sorted Array (LeetCode : 88) 💻✨ In this problem, we are given two sorted arrays. The task is to create a final sorted array inside nums1 without using extra space. I used Two Pointer Technique here 👇 🔹 i → last valid index of nums1 🔹 j → last index of nums2 🔹 k → last index of nums1 (m + n - 1) We compare from the back because if we compare from the front, the data would be overwritten. 👉 If nums1[i] > nums2[j], then we put the larger value at the back. 👉 Otherwise, we put nums2[j]. 👉 Once any one of the arrays is finished, we copy the remaining elements. Time Complexity of this approach: O(m + n) Space Complexity: O(1) (In-place solution) 🚀 This problem reminded me again — If you think in the right direction, the solution becomes much easier. #LeetCode #androidDeveloper#problemslover#dsapattern#twopointer#dailyChallenge #DSA #Java #TwoPointerTechnique #ProblemSolving #CodingJourney #SoftwareDevelopment #InPlaceAlgorithm #DataStructures #AlgorithmThinking
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 20/30 – LeetCode streak Problem: Check if Binary String Has at Most One Segment of Ones A valid string can have ones only in a single continuous block, and the input has no leading zeros, so it always starts with '1'. Core idea Since 's' starts with '1', any second segment of ones must look like: '1...1 0...0 1...1' → which necessarily contains "01" at the point where ones restart. So if "01" appears anywhere, there are at least two segments of ones → return false. If "01" never appears, all ones are in a single prefix (possibly followed by zeros), so return true. This reduces to a single substring check in 'O(n)' time and 'O(1)' space. Day 20 takeaway: Instead of explicitly counting segments, spotting the forbidden pattern "01" turns the whole logic into a one-line string check that’s still fully correct and optimal. #leetcode #dsa #java #strings #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
Appreciate your consistency brother keep going