zoukankan      html  css  js  c++  java
  • 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:

    1. 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]
      ]
      
    2. Given binary tree [3,9,8,4,0,1,7],
           3
          /
         /  
         9   8
        /  /
       /  /  
       4  01   7
      

      return its vertical order traversal as:

      [
        [4],
        [9],
        [3,0,1],
        [8],
        [7]
      ]
      
    3. Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),
           3
          /
         /  
         9   8
        /  /
       /  /  
       4  01   7
          /
         /  
         5   2
      

      return its vertical order traversal as:

      [
        [4],
        [9,5],
        [3,0,1],
        [8,2],
        [7]
      ]
      

    Analyses: Do BFS, have the root column as 0, record the leftmost column and rightmost column. Map the column as the node values in that column. 

     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> > result = new ArrayList<>();
    13         if (root == null) return result;
    14         
    15         // map the column to the nodes values in that column
    16         HashMap<Integer, List<Integer> > map = new HashMap<Integer, List<Integer> >();
    17         Queue<TreeNode> nodes = new LinkedList<TreeNode>();
    18         Queue<Integer> columns = new LinkedList<Integer>();
    19         
    20         nodes.add(root);
    21         columns.add(0);
    22         int left = 0, right = 0;
    23         while (nodes.peek() != null) {
    24             TreeNode node = nodes.remove();
    25             int column = columns.remove();
    26             
    27             if (!map.containsKey(column)) map.put(column, new ArrayList<Integer>());
    28             map.get(column).add(node.val);
    29             
    30             if (node.left != null) {
    31                 nodes.add(node.left);
    32                 columns.add(column - 1);
    33                 left = Math.min(left, column - 1);
    34             }
    35             
    36             if (node.right != null) {
    37                 nodes.add(node.right);
    38                 columns.add(column + 1);
    39                 right = Math.max(right, column + 1);
    40             }
    41         }
    42         
    43         for (int i = left; i <= right; i++) {
    44             List<Integer> temp = map.get(i);
    45             result.add(temp);
    46         }
    47         return result;
    48     }
    49 }
  • 相关阅读:
    Oracle NULL值
    关于微服务架构的权限控制初步设计
    .Net Core优化策略
    Winform如何不阻塞主进程UI的同时操作其他控件的输出
    数据结构与算法绪论学习 Day5_队列
    数据结构与算法绪论学习 Day4_栈
    数据结构与算法绪论学习 Day3_线性表
    数据结构与算法绪论学习 Day2 _算法效率的度量方法
    数据结构与算法绪论学习 Day1_基础概念
    数据结构与算法绪论学习 Day2 _线性表
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6756381.html
Copyright © 2011-2022 走看看