zoukankan      html  css  js  c++  java
  • LeetCode 987. Vertical Order Traversal of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/

    题目:

    Given a binary tree, return the vertical order traversal of its nodes values.

    For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).

    Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).

    If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

    Return an list of non-empty reports in order of X coordinate.  Every report will have a list of values of nodes.

    Example 1:

    Input: [3,9,20,null,null,15,7]
    Output: [[9],[3,15],[20],[7]]
    Explanation: 
    Without loss of generality, we can assume the root node is at position (0, 0):
    Then, the node with value 9 occurs at position (-1, -1);
    The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
    The node with value 20 occurs at position (1, -1);
    The node with value 7 occurs at position (2, -2).
    

    Example 2:

    Input: [1,2,3,4,5,6,7]
    Output: [[4],[2],[1,5,6],[3],[7]]
    Explanation: 
    The node with value 5 and the node with value 6 have the same position according to the given scheme.
    However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.

    Note:

    1. The tree will have between 1 and 1000 nodes.
    2. Each node's value will be between 0 and 1000.

    题解:

    For vertical order, we need a HashMap to maintain key as column.

    When there is same column, they should be put into same value collection.

    Here is one more extra constraint, that is to for same column and same row, have smaller value come first.

    Thus when coming out the HashMap, sort the values first based on row, then based on value.

    Time Complexity: O(n + logn*loglogn). n is the number of nodes. Thus the longest list is the height of tree m = logn. sort takes O(mlogm).

    Space: O(n).

    AC Java: 

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public List<List<Integer>> verticalTraversal(TreeNode root) {
    12         List<List<Integer>> res = new ArrayList<>();
    13         if(root == null){
    14             return res;
    15         }
    16         
    17         HashMap<Integer, List<Pair>> hm = new HashMap<>();
    18         LinkedList<Pair> que = new LinkedList<>();
    19         que.add(new Pair(0, 0, root));
    20         int min = 0;
    21         int max = 0;
    22         
    23         while(!que.isEmpty()){
    24             Pair cur = que.poll();
    25             min = Math.min(min, cur.x);
    26             max = Math.max(max, cur.x);
    27             hm.putIfAbsent(cur.x, new ArrayList<>());
    28             hm.get(cur.x).add(cur);
    29             
    30             if(cur.node.left != null){
    31                 que.add(new Pair(cur.x-1, cur.y-1, cur.node.left));
    32             }
    33             
    34             if(cur.node.right != null){
    35                 que.add(new Pair(cur.x+1, cur.y-1, cur.node.right));
    36             }
    37         }
    38         
    39         for(int i = min; i<=max; i++){
    40             List<Pair> list = hm.get(i);
    41             Collections.sort(list, (a, b) -> a.y == b.y ? a.node.val-b.node.val : b.y-a.y);
    42             
    43             List<Integer> item = new ArrayList<>();
    44             for(Pair p : list){
    45                 item.add(p.node.val);
    46             }
    47             
    48             res.add(item);
    49         }
    50         
    51         return res;
    52     }
    53 }
    54 
    55 class Pair{
    56     int x;
    57     int y;
    58     TreeNode node;
    59     public Pair(int x, int y, TreeNode node){
    60         this.x = x;
    61         this.y = y;
    62         this.node = node;
    63     }
    64     
    65     public String toString(){
    66         return "" + this.x + "^" + this.y + "^" + this.node.val;
    67     }
    68 }

    类似Binary Tree Vertical Order Traversal.

  • 相关阅读:
    mongodb复制集
    MySQL需要掌握的工具
    MHA 一主两从(传统复制)搭建-脚本VIP-自动切换
    mysqldump+binlog完整恢复被删除的表/数据库
    xtrabackup全备+binlog模拟slave恢复到某一时间点-使用position
    xtrabackup全备+binlog模拟slave恢复到某一时间点-启用GTID
    数据文件物理误误删除整个库目录处理方法
    数据文件物理误删除ibd文件处理方法
    数据文件物理误删除frm文件处理方法
    javascript技巧及常用事件方法集合(全)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12014476.html
Copyright © 2011-2022 走看看