• Leetcode: 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:
    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]
    ]
    Given binary tree [3,9,20,4,5,2,7],
        _3_
       /   
      9    20
     /    / 
    4   5 2   7
    return its vertical order traversal as:
    [
      [4],
      [9],
      [3,5,2],
      [20],
      [7]
    ]

    二叉树Vertical order traversal。这道题意思很简单但例子举得不够好,假如上面第二个例子里5还有右子树的话,就会和20在一条column里。总的来说就是假定一个node的column是 i,那么它的左子树column就是i - 1,右子树column就是i + 1。

    知道这样就好办了,我们维护一个HashMap<Integer, List<Integer>>, key是column编号,List是在该column的各个TreeNode的value,然后level order traversal, 把相应的node value加到相应的key去。我们不关心具体column编号所以取root为0列就好了,其他以此为基准。

    本题取巧的地方在于如何获得column编号,我们这里是再用了一个queue, 同步进行BFS;其他的做法可以做一个内部类

    private class TreeColumnNode{
            public TreeNode treeNode;
            int col;
            public TreeColumnNode(TreeNode node, int col) {
                this.treeNode = node;
                this.col = col;
            }
     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 public class Solution {
    11     public List<List<Integer>> verticalOrder(TreeNode root) {
    12         List<List<Integer>> res = new ArrayList<List<Integer>>();
    13         if (root == null) return res;
    14         HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
    15         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
    16         LinkedList<Integer> colQue = new LinkedList<Integer>();
    17         queue.offer(root);
    18         colQue.offer(0);
    19         map.put(0, new ArrayList<Integer>());
    20         map.get(0).add(root.val);
    21         int min=0, max=0;
    22         while (!queue.isEmpty()) {
    23             TreeNode cur = queue.poll();
    24             int col = colQue.poll();
    25             if (cur.left != null) {
    26                 queue.offer(cur.left);
    27                 colQue.offer(col-1);
    28                 if (!map.containsKey(col-1)) {
    29                     map.put(col-1, new ArrayList<Integer>());
    30                 }
    31                 map.get(col-1).add(cur.left.val);
    32                 if (col-1 < min) min = col - 1;
    33             }
    34             if (cur.right != null) {
    35                 queue.offer(cur.right);
    36                 colQue.offer(col+1);
    37                 if (!map.containsKey(col+1)) {
    38                     map.put(col+1, new ArrayList<Integer>());
    39                 }
    40                 map.get(col+1).add(cur.right.val);
    41                 if (col+1 > max) max = col + 1;
    42             }
    43         }
    44         for (int k=min; k<=max; k++) {
    45             if (map.containsKey(k))
    46                 res.add(new ArrayList<Integer>(map.get(k)));
    47         }
    48         return res;
    49     }
    50 }
  • 相关阅读:
    oracle 查询表空间
    oracle 创建表空间
    webservice SOA
    WCF初识
    win10远程桌面身份验证错误,要求的函数不受支持
    一台主机两台显示器实现方式学习
    实现Http Server学习
    lucene索引和查询文件系统存储
    java 大文件输入方式FileOutputStream
    tar
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5093131.html
Copyright © 2020-2023  润新知