314.Binary Tree Vertical Order traversal

Source: Internet
Author: User

Topic:

Given a binary tree, return the vertical order Traversal of its nodes ' values. (ie, from top to bottom, and column by column).

If the nodes is in the same row and column, the order should is from left to right.

Examples:
Given binary Tree [3,9,20,null,null,15,7] ,

    3   /   9    /   7

Return its vertical order traversal as:

[  9],  [3,15],  [+],  [7]]

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

    _3_   /     9    /\   /4   5 2   7

Return its vertical order traversal as:

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

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

Exercises

Binary Tree Vertical order traversal. The meaning of this question is very simple, but the example is not good enough, if the second example above 5 also has the right subtree, and 20 in a column. In general it is assumed that a node column is I, then its left subtree column is i-1, right subtree column is i + 1. We can use the decorator mode to build a treecolumnnode that contains a TreeNode, and a column value, and then use the level order traversal to calculate it. Use a hashmap to save the column value and the point of the same value when calculating. Also set a min column value and a max column value to make it easy to finally get the value output in HashMap from small to large order.

Time Complexity-o (n), Space complexity-o (n)

/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode rig Ht * TreeNode (int x) {val = x;} }*/ Public classSolution {Private classtreecolumnnode{ PublicTreeNode TreeNode; intCol;  PublicTreecolumnnode (TreeNode node,intCol) {             This. TreeNode =node;  This. Col =Col; }    }         PublicList<list<integer>>Verticalorder (TreeNode root) {List<List<Integer>> res =NewArraylist<>(); if(Root = =NULL) {            returnRes; } Queue<TreeColumnNode> queue =NewLinkedlist<>(); Map<integer, list<integer>> map =NewHashmap<>(); Queue.offer (NewTreecolumnnode (Root, 0)); intCurlevel = 1; intNextlevel = 0; intMin = 0; intMax = 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,NewArraylist<integer>(Arrays.aslist (Node.treeNode.val))); } curlevel--; if(Node.treeNode.left! =NULL) {Queue.offer (NewTreecolumnnode (Node.treeNode.left, NODE.COL-1)); Nextlevel++; Min= Math.min (node.col-1, Min); }            if(Node.treeNode.right! =NULL) {Queue.offer (NewTreecolumnnode (Node.treeNode.right, Node.col + 1)); Nextlevel++; Max= Math.max (Node.col + 1, Max); }            if(Curlevel = = 0) {Curlevel=Nextlevel; Nextlevel= 0; }        }                 for(inti = min; I <= Max; i++) {Res.add (Map.get (i)); }                returnRes; }}

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/

314.Binary Tree Vertical Order traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.