zoukankan      html  css  js  c++  java
  • 987. 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.
     
    314. If two nodes are in the same row and column, the order should be from left to right.
    987. If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

    time = O(n), space = O(n)
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        class Node {
            TreeNode root;
            int hd, vd;
            public Node(TreeNode root, int hd, int vd) {
                this.root = root;
                this.hd = hd;
                this.vd = vd;
            }
        }
        public List<List<Integer>> verticalTraversal(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if(root == null) {
                return res;
            }
            
            Map<Integer, List<Node>> map = new HashMap<>();
            Queue<Node> q = new LinkedList<>();
            int minHd = 0, maxHd = 0;
            
            q.offer(new Node(root, 0, 0));
            
            while(!q.isEmpty()) {
                Node cur = q.poll();
                minHd = Math.min(minHd, cur.hd);
                maxHd = Math.max(maxHd, cur.hd);
                
                map.putIfAbsent(cur.hd, new ArrayList<>());
                map.get(cur.hd).add(cur);
                
                if(cur.root.left != null) {
                    q.offer(new Node(cur.root.left, cur.hd - 1, cur.vd + 1));
                }
                if(cur.root.right != null) {
                    q.offer(new Node(cur.root.right, cur.hd + 1, cur.vd + 1));
                }
            }
            
            int idx = 0;
            for(int i = minHd; i <= maxHd; i++) {
                Collections.sort(map.get(i), (a, b) -> {
                    if(a.vd - b.vd == 0) {
                        return a.root.val - b.root.val;
                    } else {
                        return a.vd - b.vd;
                    }
                });
                res.add(new ArrayList<>());
                for(Node node : map.get(i)) {
                    res.get(idx).add(node.root.val);
                }
                idx++;
            }
            return res;
        }
    }
  • 相关阅读:
    51nod 1412 AVL树的种类(经典dp)
    HDU 6141 I am your Father!(最小树形图+权值编码)
    POJ 3164 Command Network(最小树形图模板题+详解)
    HDU 6125 Free from square(状态压缩+分组背包)
    HDU 6143 Killer Names(容斥原理)
    CSU 1808 地铁(最短路变形)
    HDU 6128 Inverse of sum(同余)
    HDU 6121 Build a tree(完全K叉树)
    HDU 6129 Just do it(杨辉三角)
    HDU 6127 Hard challenge(扫描线)
  • 原文地址:https://www.cnblogs.com/fatttcat/p/13457236.html
Copyright © 2011-2022 走看看