LeetCode 314. Binary Tree Vertical Order Traversal(二叉树垂直遍历)

Posted jmspan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 314. Binary Tree Vertical Order Traversal(二叉树垂直遍历)相关的知识,希望对你有一定的参考价值。

原题网址:https://leetcode.com/problems/binary-tree-vertical-order-traversal/

Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).

If two nodes are in the same row and column, the order should be from left to right.

Examples:

  1. Given binary tree [3,9,20,null,null,15,7],
       3
      /\\
     /  \\
     9  20
        /\\
       /  \\
      15   7
    

    return its vertical order traversal as:

    [
      [9],
      [3,15],
      [20],
      [7]
    ]
    
  2. Given binary tree [3,9,8,4,0,1,7],
         3
        /\\
       /  \\
       9   8
      /\\  /\\
     /  \\/  \\
     4  01   7
    

    return its vertical order traversal as:

    [
      [4],
      [9],
      [3,0,1],
      [8],
      [7]
    ]
    
  3. Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),
         3
        /\\
       /  \\
       9   8
      /\\  /\\
     /  \\/  \\
     4  01   7
        /\\
       /  \\
       5   2
    

    return its vertical order traversal as:

    [
      [4],
      [9,5],
      [3,0,1],
      [8,2],
      [7]
    ]

方法:以列为键值,将节点保存在哈希映射的列表中。广度优先搜索。

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x)  val = x; 
 * 
 */
public class Solution 
    public List<List<Integer>> verticalOrder(TreeNode root) 
        List<List<Integer>> results = new ArrayList<>();
        if (root == null) return results;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        Map<Integer, List<Integer>> map = new HashMap<>();
        LinkedList<Position> queue = new LinkedList<>();
        queue.add(new Position(root, 0));
        while (!queue.isEmpty()) 
            Position position = queue.remove();
            min = Math.min(min, position.column);
            max = Math.max(max, position.column);
            List<Integer> list = map.get(position.column);
            if (list == null) 
                list = new ArrayList<>();
                map.put(position.column, list);
            
            list.add(position.node.val);
            if (position.node.left != null) queue.add(new Position(position.node.left, position.column-1));
            if (position.node.right != null) queue.add(new Position(position.node.right, position.column+1));
        
        for(int i=min; i<=max; i++) 
            List<Integer> list = map.get(i);
            if (list != null) results.add(list);
        
        return results;
    

class Position 
    TreeNode node;
    int column;
    Position(TreeNode node, int column) 
        this.node = node;
        this.column = column;
    

另一种实现:

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x)  val = x; 
 * 
 */
public class Solution 
    public List<List<Integer>> verticalOrder(TreeNode root) 
        List<List<Integer>> results = new ArrayList<>();
        if (root == null) return results;
        Map<Integer, List<Integer>> map = new HashMap<>();
        LinkedList<Node> queue = new LinkedList<>();
        queue.add(new Node(root, 0));
        while (!queue.isEmpty()) 
            Node node = queue.remove();
            List<Integer> list = map.get(node.column);
            if (list == null) 
                list = new ArrayList<>();
                map.put(node.column, list);
            
            list.add(node.node.val);
            if (node.node.left != null) queue.add(new Node(node.node.left, node.column-1));
            if (node.node.right != null) queue.add(new Node(node.node.right, node.column+1));
        
        List<Integer> columns = new ArrayList<>(map.keySet());
        Collections.sort(columns);
        for(int i=0; i<columns.size(); i++) 
            results.add(map.get(columns.get(i)));
        
        return results;
    

class Node 
    TreeNode node;
    int column;
    Node(TreeNode node, int column) 
        this.node = node;
        this.column = column;
    


以上是关于LeetCode 314. Binary Tree Vertical Order Traversal(二叉树垂直遍历)的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 314. Binary Tree Vertical Order Traversal

[LeetCode] 314. Binary Tree Vertical Order Traversal

LeetCode 314. Binary Tree Vertical Order Traversal(二叉树垂直遍历)

314. Binary Tree Vertical Order Traversal

314. Binary Tree Vertical Order Traversal

314. Binary Tree Vertical Order Traversal