Binary Tree DFS Traversal and Bit Manipulation

🚀 Day 514 of #750DaysOfCode 🚀 Today’s problem: 1022. Sum of Root To Leaf Binary Numbers 🌳 This was a really nice combination of: Binary Trees DFS Traversal Bit Manipulation 🧠 Problem Insight Each root-to-leaf path in the binary tree represents a binary number. Example path: 1 → 0 → 1 Represents binary 101 = 5 Instead of storing binary as a string and converting later ❌ We can build the number directly while traversing: current = current * 2 + node.val This works because: Multiplying by 2 shifts bits left Adding node value appends the current bit Clean. Efficient. Interview-friendly. ✅ ⏱ Complexity Time: O(N) Space: O(H) (recursion stack) 💡 Key Takeaway When dealing with binary paths: 👉 Think in terms of bit shifting, not string building. Small optimization. Big impact in interviews. Consistency > Motivation. 514 days done. Still building. 💪 #LeetCode #Java #BinaryTree #DFS #BitManipulation #DataStructures #CodingJourney #Consistency

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories