LeetCode 987: Vertical Order Traversal of a Binary Tree Solution

Master LeetCode problem 987 (Vertical Order Traversal of a Binary Tree), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

987. Vertical Order Traversal of a Binary Tree

Problem Explanation

Explanation:

To solve this problem, we can perform a level-order traversal of the binary tree while keeping track of the column indices for each node. We can use a TreeMap to store the nodes based on their column indices. For each node, we update its column index and row index based on its parent's indices.

  1. Perform a level-order traversal of the binary tree using a queue.
  2. Maintain a TreeMap to store nodes based on their column indices.
  3. Update the column index and row index of each node based on its parent's indices.
  4. Sort the nodes within each column based on their values.
  5. Retrieve the vertical order traversal from the TreeMap.

Time Complexity: O(N * log(N)), where N is the number of nodes in the binary tree. Space Complexity: O(N).

:

Solution Code

import java.util.*;

class Solution {
    public List<List<Integer>> verticalTraversal(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) return result;

        TreeMap<Integer, TreeMap<Integer, List<Integer>>> map = new TreeMap<>();
        Queue<TreeNode> queue = new LinkedList<>();
        Queue<Integer> cols = new LinkedList<>();
        
        queue.offer(root);
        cols.offer(0);

        while (!queue.isEmpty()) {
            int size = queue.size();
            Map<Integer, List<Integer>> columnNodes = new HashMap<>();
            
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                int col = cols.poll();
                
                columnNodes.putIfAbsent(col, new ArrayList<>());
                columnNodes.get(col).add(node.val);
                
                if (node.left != null) {
                    queue.offer(node.left);
                    cols.offer(col - 1);
                }
                
                if (node.right != null) {
                    queue.offer(node.right);
                    cols.offer(col + 1);
                }
            }
            
            for (int key : columnNodes.keySet()) {
                map.putIfAbsent(key, new TreeMap<>());
                for (int val : columnNodes.get(key)) {
                    map.get(key).computeIfAbsent(size - 1, k -> new ArrayList<>()).add(val);
                }
            }
        }

        for (TreeMap<Integer, List<Integer>> colMap : map.values()) {
            List<Integer> column = new ArrayList<>();
            for (List<Integer> values : colMap.values()) {
                Collections.sort(values);
                column.addAll(values);
            }
            result.add(column);
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 987 (Vertical Order Traversal of a Binary Tree)?

This page provides optimized solutions for LeetCode problem 987 (Vertical Order Traversal of a Binary Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 987 (Vertical Order Traversal of a Binary Tree)?

The time complexity for LeetCode 987 (Vertical Order Traversal of a Binary Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 987 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 987 in Java, C++, or Python.

Back to LeetCode Solutions