Binary Tree Vertical Traversal with BFS and Hashmap

60 Days of Problem Solving Challenge - Day 27 👉 Today’s Problem: Vertical Tree Traversal (Binary Tree) The goal was to print the vertical order traversal of a binary tree from the leftmost vertical line to the rightmost. 💡 Approach / Logic: Used Level Order Traversal (BFS) with a queue. Assigned each node a Horizontal Distance (HD) from the root: Root → HD = 0 Left child → HD − 1 Right child → HD + 1 Stored nodes in a hashmap/dictionary where: HD → list of node values BFS ensures nodes in the same vertical line appear in level order. Finally, sorted the horizontal distances and printed values from leftmost to rightmost column. This problem reinforced concepts of Binary Trees, BFS traversal, and mapping nodes using horizontal distance. 📈 Progress Update 🔥 Current Streak: 27 Days 🎯 Goal: 60 Days of Consistent Problem Solving #60DaysOfCode #GeeksforGeeks #DSA #BinaryTree #ProblemSolving #Python #CodingJourney #Consistency

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories