[LeetCode 314] Binary Tree Vertical Order Traversal (Medium)

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

Solution - BFS,给每个节点加上distance的属性。推入Queue的时候推入的是新的Node结构(带distance属性)

  1. 以root为坐标起点,遍历整个树,找到每个点离root的距离
  2. 比如9离root的距离是-1, 20的距离是1, 15是0, 7是2
  3. 然后把距离相等的归类,最后就是
    0:【3,15】
    -1: [9]
    1: [20]
    2: [7]
    用treemap来存这个结构,以保证key是有序的

···
实际上就是BFS过程中,给每个node加一个distance的属性

  • left child离ROOT的distance,是parent distance - 1
  • right child离ROOT的distance,是parent distance + 1
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
//     以root为坐标起点,遍历整个树,找到每个点离root的距离
//     比如9离root的距离是-1, 20的距离是1
//     15是0, 7是2
//     然后把距离相等的归类,最后就是
//     0:【3,15】
//     -1: [9]
//     1: [20]
//     2: [7]
//     用treemap来存这个结构,以保证key是有序的
    
//     实际上就是BFS过程中,给每个node加一个distance的属性
//     left child离ROOT的distance,是parent distance - 1
//     right child离ROOT的distance,是parent distance + 1
    
    static class Node {
        TreeNode node;
        int distance;
        
        public Node (TreeNode node, int distance) {
            this.node = node;
            this.distance = distance;
        }
    }
    
    public List<List<Integer>> verticalOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<> ();
        if (root == null) {
            return result;
        }
        
        TreeMap<Integer, List<Integer>> tracker = new TreeMap<> ();
        Queue<Node> queue1 = new LinkedList<> ();
        Queue<Node> queue2 = new LinkedList<> ();
        
        queue1.add (new Node (root, 0));
        
        while (!queue1.isEmpty ()) {
            Node current = queue1.poll ();
            int currentNodeDistance = current.distance;
            TreeNode currentNode = current.node;
            
            List<Integer> list = tracker.getOrDefault (currentNodeDistance, new ArrayList<> ());
            list.add (currentNode.val);
            tracker.put (currentNodeDistance, list);
            
            if (currentNode.left != null) {
                queue2.add (new Node (currentNode.left, currentNodeDistance - 1));
            }
            
            if (currentNode.right != null) {
                queue2.add (new Node (currentNode.right, currentNodeDistance + 1));
            }
            
            if (queue1.isEmpty ()) {
                queue1 = queue2;
                queue2 = new LinkedList<> ();
            }
        }
        
        for (Map.Entry <Integer, List<Integer>> entry : tracker.entrySet ()) {
            result.add (entry.getValue ());
        }
        
        return result;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Description Given a binary tree, return the vertical orde...
    Nancyberry阅读 132评论 0 0
  • (十) 就在何薇怀孕期间,周小伟的父亲不知花费了多少人力物力竟然将在广州私企的儿子神奇地调回了省城的一个国有事业单...
    Silvenli阅读 368评论 0 2
  • 灵与灵的连接,感觉与感觉的连接,连接,连的是能量,接的是信念 抓核心,找重点,感知正确的连接 感恩每天睡不着可以连...
    代码数字阅读 277评论 0 0
  • 初识那年,刚好18岁,充满了对爱情,未来的憧憬,想象着未来他的模样。就这样,你闯入了我的视线,个有点腼腆,个...
    微笑的馨儿阅读 447评论 0 0
  • 做开发这么久,见多太多的设计图,能用系统解决UI的坚决不麻烦,今天回想起刚入行的那个时候,年少无知啊,说说咱们常用...
    Always_on阅读 2,979评论 3 8