leetcode 987. Vertical Order Traversal of a Binary Tree
Your code seems to have the right general structure, but is doing some extra work.

Comments would help people (including you in the future) understand what your code does and why (e.g the comment below explaining why -y is used).

Also, the problem says that nodes at the same x-coord are listed in order of their y-coord (top-to-bottom) and then by value, but you don't seem to keep track of the y-coord.

from collections import defaultdict

class Solution:
    def verticalOrder(self, root: TreeNode) -&gt; List[List]:
        # the stack keeps track of nodes that are waiting to be
        # processed, along with their x and y coordinates
        stack = [(root, 0, 0)]

        # the key is x-coordinate of a tree node, the value is a list of (-y, node_value) tuples.
        # We use -y so so that when the list is sorted later, the tuples are sorted top-to-bottom
        # and then by node_value
        node_map = defaultdict(list)

        # while there are nodes left to be processed, get the next one and add it to the node_map
        while stack:
            node, x, y = stack.pop()
            node_map[x].append((-y, node.val))

            # add the node's children to the stack for processing
            if node.left: 
                stack.append((node.left, x - 1, y - 1))

            if node.right:
                stack.append((node.right, x + 1, y - 1))

        result = []

        # process groups of nodes in order from smallest to largest x-coordinate
        for x_coord in sorted(node_map.keys()):

            # sort the tuples by y-coordinate and then value
            values_at_this_x = [node_value for _,node_value in sorted(node_map[x_coord])]

            result.append(values_at_this_x)

        return result