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.

  • 相关阅读:
    DPK880 打印机 驱动正确安装不反应。
    VB6.0 获取N位有效数字方法
    Asp.Net开发小技巧汇总
    Microsoft SqlServer生成表数据Insert语句
    dell 服务器重装
    编程应该注意
    FIRST
    SharedObject使用:在FluorineFx.net与Flex中使用共享对象维护在线用户列表实例
    Flex与As3学习笔记之:Part 3 函数参数、字符串处理、日期与时间类型
    Flex与As3学习笔记之:Part 1 Flex语言基础
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12014476.html
Copyright © 2011-2022 走看看