Given a binary tree, print it vertically. The following example illustrates vertical order traversal.
1 / 2 3 /\ / 4 5 6 7 \ 8 9
We strongly recommend to minimize the browser and try this yourself first.
The idea was to traverse the tree once and get the minimum and maximum horizontal distance with respect to root. For the tree shown above, minimum distance is-2 (for node with value 4) and maximum distance are 3 (for node with value 9) .
Once we have maximum and minimum distances from root, we iterate for each vertical line at distance minimum to maximum fro M root, and for each of the vertical line traverse the tree and print the nodes which lie on this vertical line.
Algorithm:
Min--Minimum horizontal distance from root//max---Maximum horizontal distance from root//HD FINDMI NMax (tree, Min, max, HD) If tree is NULL and then return; If HD is less than min then min = HD; else if HD is greater than Max then *max = HD; Findminmax (tree->left, Min, Max, hd-1); printverticalline (tree, Line_no, HD) If tree is a NULL then return; If HD is equal to Line_no and then print (tree->data); Printverticalline (Tree->left, Line_no, hd-1);
1 //Print a Binary Tree in Vertical Order2 Static intmin;3 Static intMax;4 StaticHashmap<integer,arraylist>map;5 6 Public Static voidGenerate (TreeNode root, Integer dis) {7 if(Root = =NULL)return;8 Else{9 if(Map.containskey (DIS)) map.get (DIS). Add (root.val);Ten Else{ Onearraylist<integer> tmp =NewArraylist<integer> (); A Tmp.add (root.val); - map.put (DIS, tmp); - } the } -Generate (Root.left, dis-1); -Generate (root.right, dis + 1); - } + - Public StaticArraylist<integer>[] VERTICALORDERTRAVERALBT (TreeNode root) { +min = 0; Max = 0; A intLen = max-min + 1; atMap =NewHashmap<integer,arraylist>(); -Generate (Root, 0); - returnmap.values (); - } - - in Public Static voidMain (string[] args) { -TreeNode root =NewTreeNode (1); toRoot.left =NewTreeNode (2); +Root.right =NewTreeNode (3); -Root.left.left =NewTreeNode (4); theRoot.left.right =NewTreeNode (5); *Root.right.left =NewTreeNode (6); $Root.right.right =NewTreeNode (7);Panax NotoginsengRoot.right.left.right =NewTreeNode (8); -Root.right.right.right =NewTreeNode (9); theArraylist[] result =VERTICALORDERTRAVERALBT (root); + for(inti = 0; I < Max-min + 1; i + +){ A System.out.println (result[i].tostring ()); the } +}
You can also use HashMap to do so that you do not need to min/max.
1 //Print a Binary Tree in Vertical Order2 Static intmin;3 Static intMax;4 StaticArraylist<integer>[] output;5 Public Static voidFindminmax (TreeNode root, Integer dis) {6 if(Root = =NULL)return;7 Else{8Min =math.min (Dis, min);9Max =math.max (DIS, max);Ten } OneFindminmax (Root.left, dis-1); AFindminmax (root.right, dis + 1); - } - the Public Static voidGenerate (TreeNode root, Integer dis) { - if(Root = =NULL)return; - Else{ -Output[dis-Min].add (root.val); + } -Generate (Root.left, dis-1); +Generate (root.right, dis + 1); A } at - Public StaticArraylist<integer>[] VERTICALORDERTRAVERALBT (TreeNode root) { -min = 0; Max = 0; -Findminmax (Root, 0); - intLen = max-min + 1; -Output =NewArraylist[len]; in for(inti = 0; i < Len; i + +){ -Output[i] =NewArraylist<integer>(); to } +Generate (Root, 0); - returnoutput; the } * $ Panax Notoginseng Public Static voidMain (string[] args) { -TreeNode root =NewTreeNode (1); theRoot.left =NewTreeNode (2); +Root.right =NewTreeNode (3); ARoot.left.left =NewTreeNode (4); theRoot.left.right =NewTreeNode (5); +Root.right.left =NewTreeNode (6); -Root.right.right =NewTreeNode (7); $Root.right.left.right =NewTreeNode (8); $Root.right.right.right =NewTreeNode (9); -Arraylist[] result =VERTICALORDERTRAVERALBT (root); - for(inti = 0; I < Max-min + 1; i + +){ the System.out.println (result[i].tostring ()); - }Wuyi}
Print a Binary Tree in Vertical Order