Subbareddy karri’s Post

Ever wondered what a binary tree looks like from the right side? 🌳👀 Let’s find out! Hey everyone! Day 300 of my 365-day coding journey brought me to a classic tree traversal challenge: LeetCode's "Binary Tree Right Side View." This problem is a great way to understand level-order traversal and how to extract meaningful patterns from it. Let’s dive in. ⚡ 🛠️ The Problem Given the root of a binary tree, imagine standing on the right side of it — your goal is to return the values of the nodes you can see, ordered from top to bottom. In simple terms, at each level of the tree, you only see the rightmost node. 🌲➡️ 🎯 The Approach I solved this using two efficient variations of Breadth-First Search (BFS), both leveraging a queue for level-order traversal. Solution 1: BFS using Queue (with pop) - Used a queue to process nodes level by level. - Captured the queue size before processing each level. - The last node in each level is the one visible from the right side — added it to the result. Solution 2: BFS using Queue (without pop, using indexing) - Instead of popping nodes, I accessed them by their level range indices. - The last node in each level’s range becomes the right-side view node. - Avoids extra pop operations, keeping traversal clean and efficient. 🧠 Key Takeaways 🌿 BFS is the go-to method for problems that involve levels or hierarchies in trees.   🌿 Tracking level boundaries using queue size is a simple yet powerful trick.   🌿 You can also solve this using DFS by recording the last node visited at each depth — multiple ways, same goal! 💡 Challenge for You! Do you prefer BFS or DFS for tree traversal problems?   If you’re a BFS fan, do you use the queue size trick or a sentinel marker to detect level changes? Drop your thoughts below! 💬 📺 Watch My Full Walkthrough I explain both BFS techniques step-by-step in my latest video: https://lnkd.in/gx3xz9mi 🔥 Join the Conversation If you're exploring data structures and algorithms like me, let’s connect! Always great to share ideas and learn together. 🚀 #CodingJourney #DSA #LeetCode #BinaryTree #TreeTraversal #BFS #ProblemSolving #Algorithms #DataStructures #JavaScript #LearningEveryDay #DeveloperLife #Programming #CodeNewbies #TechLearning #365DaysOfCode

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories