zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ASP.NET 实现邮件发送和接受的功能(Sockets)
    使用Sql server进行分布式查询
    Sqlserver中的一些技巧
    使用sql server中的全文索引
    水晶报表的装载和修改文本
    创建作业的通用存储过程
    MS SQL数据库备份和恢复
    数据库运用XML操作
    安装程序自动安装数据库
    ASP.NET 实现邮件发送和接受的功能(Mail)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5093131.html
Copyright © 2011-2022 走看看