Problem statement

https://binarysearch.com/problems/Left-Side-View-of-a-Tree/

Solution

Equal to Leetcode 0199. Binary Tree Right Side View, just change left to right.

Complexity

Time complexity is O(n) for classical dfs, space is O(h), again classical complexity for dfs as well as this amount of space we need to keep in our answer.

Code

class Solution:
    def solve(self, root):
        ans = {}
        def dfs(node, H):
            if not node: return 
            
            dfs(node.left, H + 1)
            if H not in ans: ans[H] = node.val
            dfs(node.right, H + 1)
            
        dfs(root, 0)
        return [ans[i] for i in range(len(ans))]