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

 

Given binary tree [3,9,20,4,5,2,7],

    _3_
   /   \
  9    20
 / \   / \
4   5 2   7

 

return its vertical order traversal as:

[
  [4],
  [9],
  [3,5,2],
  [20],
  [7]
]

链接: http://leetcode.com/problems/binary-tree-vertical-order-traversal/

题解:

二叉树Vertical order traversal。这道题意思很简单但例子举得不够好,假如上面第二个例子里5还有右子树的话,就会和20在一条column里。总的来说就是假定一个node的column是 i,那么它的左子树column就是i - 1,右子树column就是i + 1。我们可以用decorator模式建立一个TreeColumnNode,包含一个TreeNode,以及一个column value,然后用level order traversal进行计算就可以了,计算的时候用一个HashMap保存column value以及相同value的点。也要设置一个min column value和一个max column value,方便最后按照从小到大顺序获取hashmap里的值输出。这道题Discuss区Yavinci大神写得非常棒,放在reference里。

Time Complexity - O(n),  Space Complexity - O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private class TreeColumnNode{
        public TreeNode treeNode;
        int col;
        public TreeColumnNode(TreeNode node, int col) {
            this.treeNode = node;
            this.col = col;
        }
    }
    
    public List<List<Integer>> verticalOrder(TreeNode root) {    
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) {
            return res;
        }
        Queue<TreeColumnNode> queue = new LinkedList<>();
        Map<Integer, List<Integer>> map = new HashMap<>();
        queue.offer(new TreeColumnNode(root, 0));
        int curLevel = 1;
        int nextLevel = 0;
        int min = 0;
        int max = 0;
        
        while(!queue.isEmpty()) {
            TreeColumnNode node = queue.poll();
            if(map.containsKey(node.col)) {
                map.get(node.col).add(node.treeNode.val);
            } else {
                map.put(node.col, new ArrayList<Integer>(Arrays.asList(node.treeNode.val)));
            }
            curLevel--;
            
            if(node.treeNode.left != null) {
                queue.offer(new TreeColumnNode(node.treeNode.left, node.col - 1));
                nextLevel++;
                min = Math.min(node.col - 1, min);
            }
            if(node.treeNode.right != null) {
                queue.offer(new TreeColumnNode(node.treeNode.right, node.col + 1));
                nextLevel++;
                max = Math.max(node.col + 1, max);
            }
            if(curLevel == 0) {
                curLevel = nextLevel;
                nextLevel = 0;
            }
        }
        
        for(int i = min; i <= max; i++) {
            res.add(map.get(i));
        }
        
        return res;
    }
}

 

Reference:

https://leetcode.com/discuss/75054/5ms-java-clean-solution

https://leetcode.com/discuss/73113/using-hashmap-bfs-java-solution

https://leetcode.com/discuss/74022/hashmap-bfs-solution-in-java

https://leetcode.com/discuss/73893/java-level-order-traversal-solution

http://algorithms.tutorialhorizon.com/print-the-binary-tree-in-vertical-order-path/

http://www.geeksforgeeks.org/print-binary-tree-vertical-order/

http://www.geeksforgeeks.org/print-binary-tree-vertical-order-set-2/

posted @ 2015-12-22 05:59  YRB  阅读(9331)  评论(2编辑  收藏  举报