Print Nodes in Top View of Binary Tree

Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. Expected time complexity is O(n)

A node x is there in output if x is the topmost node at its horizontal distance. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1.

       1
    /     \
   2       3
  /  \    / \
 4    5  6   7
Top view of the above binary tree is
4 2 1 3 7

        1
      /   \
    2       3
      \   
        4  
          \
            5
             \
               6
Top view of the above binary tree is
2 1 3 6

We strongly recommend to minimize your browser and try this yourself first.

The idea is to do 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 that the topmost node at a horizontal node is visited before any other node of same horizontal distance below it. Hashing is used to check if a node at given horizontal distance is seen or not.

 

 1 /Print a Binary Tree in Vertical Order 
 2 static int min;
 3 static int max;
 4 static int[] output;
 5 
 6 public class Item{
 7     public Integer dis;
 8     public TreeNode root;
 9     public Item(Integer dis, TreeNode root){
10         this.root = root;
11         this.dis = dis;
12     }
13 }
14     static int min;
15     static int max;
16     static int[] output;
17     
18     public static void findMinMax(TreeNode root, Integer dis){
19         if(root == null) return;
20         else{
21             min = Math.min(dis, min);
22             max = Math.max(dis, max);
23         }
24         findMinMax(root.left, dis - 1);
25         findMinMax(root.right, dis + 1);
26     }
27 
28     public static void levelOrder(TreeNode root){
29         LinkedList<Item> queue = new LinkedList<Item>();
30         queue.add(new Item(0, root));
31         while(!queue.isEmpty()){
32             Item tmp = queue.poll();
33 //            if(output[tmp.dis - min] == 0){
34                 output[tmp.dis - min] = tmp.root.val;
35 //            }
36             if(tmp.root.left != null) queue.add(new Item(tmp.dis - 1, tmp.root.left));
37             if(tmp.root.right != null) queue.add(new Item(tmp.dis + 1, tmp.root.right));
38         }
39     }
40 
41     public static int[] verticalOrderTraveralBT(TreeNode root){
42         min = 0; max = 0;
43         findMinMax(root, 0);
44         int len = max - min + 1;
45         output = new int[len];
46         levelOrder(root);
47         return output;
48     }
49 
50 
51     public static void main(String[] args) {
52 //        int[] p = new int[]{10, 20, 30, 40, 30};
53 //        System.out.println(MatrixChainMultiplication(p));
54         
55         
56         TreeNode root = new TreeNode(1);
57         root.left = new TreeNode(2);
58         root.right = new TreeNode(3);
59         root.left.left = new TreeNode(4);
60         root.left.right = new TreeNode(5);
61         root.right.left = new TreeNode(6);
62         root.right.right = new TreeNode(7);
63         root.right.left.right = new TreeNode(8);
64         root.right.right.right = new TreeNode(9);
65         
66         /* Create following Binary Tree
67         1
68       /  \
69      2    3
70       \
71        4
72         \
73          5
74           \
75            6*/
76 //   TreeNode root = new TreeNode(1);
77 //   root.left = new TreeNode(2);
78 //   root.right = new TreeNode(3);
79 //   root.left.right = new TreeNode(4);
80 //   root.left.right.right = new TreeNode(5);
81 //   root.left.right.right.right = new TreeNode(6);
82         int[] result = verticalOrderTraveralBT(root);
83         System.out.println(result);
84     }

如果是top view 就把 if(output[tmp.dis - min] == 0){ uncomment

 

posted on 2015-03-15 01:42  Step-BY-Step  阅读(300)  评论(0编辑  收藏  举报

导航