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

    Input: [3,9,20,null,null,15,7]
    
       3
      /
     /  
     9  20
        /
       /  
      15   7 
    
    Output:
    
    [
      [9],
      [3,15],
      [20],
      [7]
    ]
    

    Examples 2:

    Input: [3,9,8,4,0,1,7]
    
         3
        /
       /  
       9   8
      /  /
     /  /  
     4  01   7 
    
    Output:
    
    [
      [4],
      [9],
      [3,0,1],
      [8],
      [7]
    ]
    

    Examples 3:

    Input: [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
    
    Output:
    
    [
      [4],
      [9,5],
      [3,0,1],
      [8,2],
      [7]
    ]

    思路:level order traversal (BFS) + hash table,ref: https://www.youtube.com/watch?v=PQKkr036wRc

    定义horizontal distance: 从root出发,Hd(root) = 0, Hd(左孩子) = Hd(parent) - 1, Hd(右孩子) = Hd(parent) + 1

    需要两个hash map,dist记录每个节点的hd, map记录每个dist对应的节点的值。先把root放入队列,在dist, map中更新root的相关信息。当队列不为空时,取出队列首元素,check其左右孩子是否存在并更新子节点的hd(更新dist 和 map)。同时还需要两个常数min, max记录hd的范围,以便最后能按顺序把map中的list输出

    time: O(n), space: O(n)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<List<Integer>> verticalOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if(root == null) {
                return res;
            }
            
            Map<Integer, List<Integer>> map = new HashMap<>();
            Map<TreeNode, Integer> dist = new HashMap<>();
            Queue<TreeNode> q = new LinkedList<>();
            int min = 0, max = 0;
            
            q.offer(root);
            dist.put(root, 0);
            map.put(0, new ArrayList<>());
            map.get(0).add(root.val);
            
            while(!q.isEmpty()) {
                TreeNode cur = q.poll();
                int dis = dist.get(cur);
                if(cur.left != null) {
                    dist.put(cur.left, dis - 1);
                    map.putIfAbsent(dis - 1, new ArrayList<>());
                    map.get(dis - 1).add(cur.left.val);
                    min = Math.min(min, dis - 1);
                    q.offer(cur.left);
                }
                if(cur.right != null) {
                    dist.put(cur.right, dis + 1);
                    map.putIfAbsent(dis + 1, new ArrayList<>());
                    map.get(dis + 1).add(cur.right.val);
                    max = Math.max(max, dis + 1);
                    q.offer(cur.right);
                }
            }
    
            for(int i = min; i <= max; i++) {
                res.add(map.get(i));
            }
            return res;
        }
    }
  • 相关阅读:
    WCF学习笔记之传输安全
    JavaScript –type
    详解android的号码匹配
    list和用vector区别(Vector相当于是数组,读写快,插入慢)
    拒绝收购邀请,三年专注开发,开源的私有云盘“迷你云”(十人团队在三年时间里靠自筹资金专注开发出来的作品)
    无功不受禄
    如何判断是否安装了VC RUNTIME
    GExpert 1.38 实验版含经典代码格式工具 Berlin 编译版
    初始化IoC容器(Spring源码阅读)
    JavaScript函数绑定
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10247929.html
Copyright © 2011-2022 走看看