题目

Given a binary tree, return the vertical order traversal of its nodes values.

For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).

Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).

If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.

Example 1:
image.png

  1. Input: [3,9,20,null,null,15,7]
  2. Output: [[9],[3,15],[20],[7]]
  3. Explanation:
  4. Without loss of generality, we can assume the root node is at position (0, 0):
  5. Then, the node with value 9 occurs at position (-1, -1);
  6. The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
  7. The node with value 20 occurs at position (1, -1);
  8. The node with value 7 occurs at position (2, -2).

Example 2:
image.png

  1. Input: [1,2,3,4,5,6,7]
  2. Output: [[4],[2],[1,5,6],[3],[7]]
  3. Explanation:
  4. The node with value 5 and the node with value 6 have the same position according to the given scheme.
  5. However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.

Note:

  1. The tree will have between 1 and 1000 nodes.
  2. Each node’s value will be between 0 and 1000.

题意

定义二叉树中一个结点的横坐标为x,纵坐标为y,则其左子结点坐标为(x-1, y-1),其右子结点坐标为(x+1, y-1),求该二叉树的纵向遍历,类似于层序遍历,x相同的结点属于同一层,不同层按x升序排列,同一层结点按y降序排列。

思路

层序遍历所有结点,这样可以保证y值是递减的;结点的x轴信息可以用一个与层序遍历同步的队列来记录;最终结果可以用一个(x->List)的map保存。注意同一层x值相同的两个结点需要按照值大小排列,因此每一层可以再开一个map进行处理,再合并到结果map中。


代码实现

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>> verticalTraversal(TreeNode root) {
  18. List<List<Integer>> ans = new ArrayList<>();
  19. Map<Integer, List<Integer>> hash = new TreeMap<>();
  20. Map<Integer, Set<Integer>> tmp = new HashMap<>();
  21. Queue<TreeNode> q = new LinkedList<>();
  22. Queue<Integer> coord = new LinkedList<>();
  23. if (root != null) {
  24. q.offer(root);
  25. coord.offer(0);
  26. }
  27. while (!q.isEmpty()) {
  28. int size = q.size();
  29. tmp.clear();
  30. for (int i = 0; i < size; i++) {
  31. TreeNode cur = q.poll();
  32. int x = coord.poll();
  33. tmp.putIfAbsent(x, new TreeSet<>());
  34. tmp.get(x).add(cur.val);
  35. if (cur.left != null) {
  36. q.offer(cur.left);
  37. coord.offer(x - 1);
  38. }
  39. if (cur.right != null) {
  40. q.offer(cur.right);
  41. coord.offer(x + 1);
  42. }
  43. }
  44. for (int x : tmp.keySet()) {
  45. hash.putIfAbsent(x, new ArrayList());
  46. hash.get(x).addAll(tmp.get(x));
  47. }
  48. }
  49. for (List<Integer> list : hash.values()) {
  50. ans.add(list);
  51. }
  52. return ans;
  53. }
  54. }

JavaScript

  1. /**
  2. * @param {TreeNode} root
  3. * @return {number[][]}
  4. */
  5. var verticalTraversal = function (root) {
  6. const xList = new Map()
  7. const q = []
  8. const x = []
  9. q.push(root)
  10. x.push(0)
  11. while (q.length) {
  12. const size = q.length
  13. const tmp = new Map()
  14. for (let i = 0; i < size; i++) {
  15. const cur = q.shift()
  16. const curX = x.shift()
  17. !tmp.has(curX) && tmp.set(curX, [])
  18. tmp.get(curX).push(cur.val)
  19. if (cur.left) {
  20. q.push(cur.left)
  21. x.push(curX - 1)
  22. }
  23. if (cur.right) {
  24. q.push(cur.right)
  25. x.push(curX + 1)
  26. }
  27. }
  28. for (const [key, value] of tmp.entries()) {
  29. value.sort((a, b) => a - b)
  30. !xList.has(key) && xList.set(key, [])
  31. xList.get(key).push(...value)
  32. }
  33. }
  34. return Array.from(xList.keys())
  35. .sort((a, b) => a - b)
  36. .map(key => xList.get(key))
  37. }