码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 314. Binary Tree Vertical Order Traversal

时间:2020-05-13 09:40:41      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:from   时间   顺序   col   空间   tree node   poll   实现   output   

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]
]

二叉树的垂直遍历。题目要求很直白,思路是BFS层序遍历,但是需要用到一个index变量记录子节点相对于根节点的偏移量,同时需要用hashmap<偏移量,相同偏移量的节点组成的list>把相同偏移量的节点放在一起。

首先BFS层序遍历还是跟一般的BFS差不多,但是这里我做了两个queue,一个存放节点,一个存放每个节点的偏移量,这样BFS遍历的时候,可以同时得到被遍历节点的偏移量。min和max的存在是记录最左和最右的偏移量,这样最后输出res的时候就可以从左往右按顺序输出了。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public List<List<Integer>> verticalOrder(TreeNode root) {
18         List<List<Integer>> res = new ArrayList<>();
19         if (root == null) {
20             return res;
21         }
22 
23         Map<Integer, ArrayList<Integer>> map = new HashMap<>();
24         Queue<TreeNode> q = new LinkedList<>();
25         Queue<Integer> cols = new LinkedList<>();
26         q.add(root);
27         cols.add(0);
28         int min = 0;
29         int max = 0;
30         while (!q.isEmpty()) {
31             TreeNode node = q.poll();
32             int col = cols.poll();
33             if (!map.containsKey(col)) {
34                 map.put(col, new ArrayList<Integer>());
35             }
36             map.get(col).add(node.val);
37             if (node.left != null) {
38                 q.add(node.left);
39                 cols.add(col - 1);
40                 min = Math.min(min, col - 1);
41             }
42             if (node.right != null) {
43                 q.add(node.right);
44                 cols.add(col + 1);
45                 max = Math.max(max, col + 1);
46             }
47         }
48 
49         for (int i = min; i <= max; i++) {
50             res.add(map.get(i));
51         }
52         return res;
53     }
54 }

 

LeetCode 题目总结

[LeetCode] 314. Binary Tree Vertical Order Traversal

标签:from   时间   顺序   col   空间   tree node   poll   实现   output   

原文地址:https://www.cnblogs.com/cnoodle/p/12880052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!