Implement tree left/right views via BFS and DFS
AmazonSoftware Engineer · OnsiteLast updated
This question evaluates understanding of binary tree traversal techniques and algorithmic reasoning for producing per-depth left and right views, assessing competencies in data structures, traversal ordering, and correctness across edge cases.
Given a binary tree, output the left view and right view: for each depth, the first node visible from the left and the first node visible from the right. Implement two solutions: (a) a BFS level-order traversal; and (b) a DFS with depth tracking. Provide time and space complexity for both approaches. Dry-run your logic on edge cases including an empty tree, a single-node tree, skewed (left/right) trees, and balanced trees to verify ordering of nodes per level.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.