Top view of a binary tree is the set of nodes visible if the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can printed in any order. expected time complexity is O (n)
A node x is there in output if x are the topmost node at its horizontal distance. Horizontal distance of left child of a node x are equal to horizontal distance of x minus 1, and that's right child is Hor Izontal distance of x plus 1.
1 / 2 3 / \ / 4 5 6 7Top View of the above binary tree IS4 2 1 3 7 1 / 2 3 \ 4 5 6Top View of the above binary tree Is2 1 3 6
We strongly recommend to minimize your browser and try this yourself first.
Something similar to vertical Order traversal. Like vertical Order traversal, we need to nodes of same horizontal distance together. We do a level order traversal so, the topmost node at a horizontal node was visited before any other node of same Horiz Ontal distance below it. Hashing is used to check if a node at given horizontal distance are seen or not.
1/Print a Binary Tree in Vertical Order2 Static intmin;3 Static intMax;4 Static int[] output;5 6 Public classitem{7 PublicInteger dis;8 PublicTreeNode Root;9 PublicItem (Integer dis, TreeNode root) {Ten This. root =Root; One This. Dis =dis; A } - } - Static intmin; the Static intMax; - Static int[] output; - - Public Static voidFindminmax (TreeNode root, Integer dis) { + if(Root = =NULL)return; - Else{ +Min =math.min (Dis, min); AMax =math.max (DIS, max); at } -Findminmax (Root.left, dis-1); -Findminmax (root.right, dis + 1); - } - - Public Static voidLevelorder (TreeNode root) { inlinkedlist<item> queue =NewLinkedlist<item>(); -Queue.add (NewItem (0, Root)); to while(!Queue.isempty ()) { +Item tmp =Queue.poll (); - //if (output[tmp.dis-min] = = 0) { theOutput[tmp.dis-min] =Tmp.root.val; * // } $ if(Tmp.root.left! =NULL) Queue.add (NewItem (tmp.dis-1, Tmp.root.left));Panax Notoginseng if(Tmp.root.right! =NULL) Queue.add (NewItem (Tmp.dis + 1, Tmp.root.right)); - } the } + A Public Static int[] VERTICALORDERTRAVERALBT (TreeNode root) { themin = 0; Max = 0; +Findminmax (Root, 0); - intLen = max-min + 1; $Output =New int[Len]; $ Levelorder (root); - returnoutput; - } the - Wuyi Public Static voidMain (string[] args) { the //int[] p = new int[]{10, . - //System.out.println (Matrixchainmultiplication (P)); Wu - AboutTreeNode root =NewTreeNode (1); $Root.left =NewTreeNode (2); -Root.right =NewTreeNode (3); -Root.left.left =NewTreeNode (4); -Root.left.right =NewTreeNode (5); ARoot.right.left =NewTreeNode (6); +Root.right.right =NewTreeNode (7); theRoot.right.left.right =NewTreeNode (8); -Root.right.right.right =NewTreeNode (9); $ the /*Create following Binary Tree the 1 the / the 2 3 - in 4 the the 5 About the 6*/ the //TreeNode root = new TreeNode (1); the //root.left = new TreeNode (2); + //root.right = new TreeNode (3); - //root.left.right = new TreeNode (4); the //root.left.right.right = new TreeNode (5);Bayi //root.left.right.right.right = new TreeNode (6); the int[] result =VERTICALORDERTRAVERALBT (root); the System.out.println (result); -}
If it is top view, put if (output[tmp.dis-min] = = 0) {Uncomment
Print Nodes in Top View of Binary Tree