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:

    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]
      ]
      
     
     Analysis:
     
    Note that in the second example, 2 is in the left subtree of root, but will after 8 in the results. So simple traverse may not work. We can use BFS with index recording.
     
    Solution:
     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>> resList = new ArrayList<List<Integer>>();
    13         if (root==null) return resList;
    14 
    15         List<TreeNode> nodeList = new ArrayList<TreeNode>();
    16         List<Integer> indexList = new ArrayList<Integer>();
    17         int cur = 0, min = 0, max=0;
    18         nodeList.add(root);
    19         indexList.add(0);
    20     
    21         while (cur<nodeList.size()){
    22             TreeNode curNode = nodeList.get(cur);
    23             int curIndex = indexList.get(cur);
    24             if (curIndex < min) min = curIndex;
    25             if (curIndex > max) max = curIndex;
    26 
    27             if (curNode.left!=null){
    28                 nodeList.add(curNode.left);
    29                 indexList.add(curIndex-1);
    30             }
    31             if (curNode.right!=null){
    32                 nodeList.add(curNode.right);
    33                 indexList.add(curIndex+1);
    34             }
    35             cur++;
    36         }
    37         
    38         for (int i=min;i<=max;i++) resList.add(new LinkedList<Integer>());
    39         
    40         for (int i=0;i<nodeList.size();i++){
    41             resList.get(indexList.get(i)-min).add(nodeList.get(i).val);
    42         }
    43 
    44         
    45         return resList;
    46     }
    47         
    48 }
  • 相关阅读:
    献给初学者:谈谈如何学习Linux操作系统
    Spring MVC 教程,快速入门,深入分析
    缓存、缓存算法和缓存框架简介
    计算机科学中最重要的32个算法
    12岁的少年教你用Python做小游戏
    当你输入一个网址的时候,实际会发生什么?
    java程序员从笨鸟到菜鸟之(七)一—java数据库操作
    java中的类修饰符、成员变量修饰符、方法修饰符。
    Java内存分配全面浅析
    Java知多少(19)访问修饰符(访问控制符)
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5725751.html
Copyright © 2011-2022 走看看