Vertical Order Traversal of a Binary Tree

52 篇文章 0 订阅
36 篇文章 0 订阅

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:

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

思路:这题跟314 Binary Tree Vertical Order Traversal 不同的是:

Difference:
314. If two nodes are in the same row and column, the order should be from left to right.
987. If two nodes have the same position, then the value of the node that is reported first is the value that is smaller. 

When two nodes have the same position (i.e. same X and same Y value), 314 asks us to sort them in the result based on X ("from left to right"), while 987 asks us to sort them in the result based on the nodes' values.

方法就是加个不一样的comparator就可以解决问题了。代码由314 Binary Tree Vertical Order Traversal 更改得来。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public class Node {
        int x;
        int y;
        int value;
        public Node(int x, int y, int value) {
            this.x = x;
            this.y = y;
            this.value = value;
        }
    }
    
    public List<List<Integer>> verticalTraversal(TreeNode root) {
         List<List<Integer>> lists = new ArrayList<List<Integer>>();
        if(root == null) {
            return lists;
        }
        HashMap<Integer, List<Node>> hashmap = new HashMap<>();
        Queue<TreeNode> nqueue = new LinkedList<>();
        Queue<Integer> cqueue = new LinkedList<>();
        nqueue.offer(root);
        cqueue.offer(0);
        
        int minIndex = Integer.MAX_VALUE;
        int maxIndex = Integer.MIN_VALUE;
        
        int level = 0;
        
        while(!nqueue.isEmpty()) {
            int size = nqueue.size();
            for(int i = 0; i < size; i++) {
                TreeNode node = nqueue.poll();
                Integer index = cqueue.poll();
                hashmap.putIfAbsent(index, new ArrayList<Node>());
                hashmap.get(index).add(new Node(level, index, node.val));

                minIndex = Math.min(minIndex, index);
                maxIndex = Math.max(maxIndex, index);

                if(node.left != null) {
                    nqueue.offer(node.left);
                    cqueue.offer(index - 1);
                }
                if(node.right != null) {
                    nqueue.offer(node.right);
                    cqueue.offer(index + 1);
                } 
            }
            level++;
        }
        
        for(int i = minIndex; i <= maxIndex; i++) {
            List<Node> list = hashmap.get(i);
            Collections.sort(list, (a, b) -> {
                if(a.x != b.x) {
                    return a.x - b.x;
                } else if(a.y != b.y) {
                    return a.y - b.y;
                } else {
                    return a.value - b.value;
                }
            });
            List<Integer> res = new ArrayList<>();
            for(Node n: list) {
                res.add(n.value);
            }
            lists.add(res);
        }
        return lists;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值