Print a Binary Tree in Vertical Order

本文介绍了一种方法来垂直打印二叉树,通过一次遍历获取树的最小和最大水平距离,然后按照从根节点到每个垂直线的距离进行打印。

Given a binary tree, print it vertically. The following example illustrates vertical order traversal.

           1
        /    \
       2      3
      / \    / \
     4   5  6   7
             \   \
              8   9 
               
			  
The output of print this tree vertically will be:
4
2
1 5 6
3 8
7
9 

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

The idea is 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 is 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 from root, and for each vertical line traverse the tree and print the nodes which lie on that vertical line.

Algorithm:

// min --> Minimum horizontal distance from root
// max --> Maximum horizontal distance from root
// hd  --> Horizontal distance of current node from root 
findMinMax(tree, min, max, hd)
     if tree is NULL 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);
     findMinMax(tree->right, min, max, hd+1);

 
printVerticalLine(tree, line_no, hd)
     if tree is NULL then return;
 
     if hd is equal to line_no, then
           print(tree->data);
     printVerticalLine(tree->left, line_no, hd-1);
     printVerticalLine(tree->right, line_no, hd+1); 

 1 //Print a Binary Tree in Vertical Order 
 2 static int min;
 3 static int max;
 4 static HashMap<Integer,ArrayList> map;
 5 
 6 public static void generate(TreeNode root, Integer dis){
 7     if(root == null) return;
 8     else{
 9         if(map.containsKey(dis)) map.get(dis).add(root.val);
10         else{
11             ArrayList<Integer> tmp = new ArrayList<Integer> ();
12             tmp.add(root.val);
13             map.put(dis, tmp);
14         }
15     }
16     generate(root.left, dis - 1);
17     generate(root.right, dis + 1);
18 }
19 
20 public static ArrayList<Integer>[] verticalOrderTraveralBT(TreeNode root){
21     min = 0; max = 0;
22     int len = max - min + 1;
23     map = new HashMap<Integer,ArrayList>();
24     generate(root, 0);
25     return map.values();
26 }
27 
28 
29 public static void main(String[] args) {    
30     TreeNode root = new TreeNode(1);
31     root.left = new TreeNode(2);
32     root.right = new TreeNode(3);
33     root.left.left = new TreeNode(4);
34     root.left.right = new TreeNode(5);
35     root.right.left = new TreeNode(6);
36     root.right.right = new TreeNode(7);
37     root.right.left.right = new TreeNode(8);
38     root.right.right.right = new TreeNode(9);
39     ArrayList[] result = verticalOrderTraveralBT(root);
40     for(int i = 0; i < max - min + 1; i ++){
41         System.out.println(result[i].toString());
42     }
43 }

也可以使用HashMap 来做, 这样就不需要min / max 了。

 1 //Print a Binary Tree in Vertical Order 
 2 static int min;
 3 static int max;
 4 static ArrayList<Integer>[] output;
 5 public static void findMinMax(TreeNode root, Integer dis){
 6     if(root == null) return;
 7     else{
 8         min = Math.min(dis, min);
 9         max = Math.max(dis, max);
10     }
11     findMinMax(root.left, dis - 1);
12     findMinMax(root.right, dis + 1);
13 }
14 
15 public static void generate(TreeNode root, Integer dis){
16     if(root == null) return;
17     else{
18         output[dis - min].add(root.val);
19     }
20     generate(root.left, dis - 1);
21     generate(root.right, dis + 1);
22 }
23 
24 public static ArrayList<Integer>[] verticalOrderTraveralBT(TreeNode root){
25     min = 0; max = 0;
26     findMinMax(root, 0);
27     int len = max - min + 1;
28     output = new ArrayList[len];
29     for(int i = 0; i < len; i ++){
30         output[i] = new ArrayList<Integer>();
31     }
32     generate(root, 0);
33     return output;
34 }
35 
36 
37 public static void main(String[] args) {    
38     TreeNode root = new TreeNode(1);
39     root.left = new TreeNode(2);
40     root.right = new TreeNode(3);
41     root.left.left = new TreeNode(4);
42     root.left.right = new TreeNode(5);
43     root.right.left = new TreeNode(6);
44     root.right.right = new TreeNode(7);
45     root.right.left.right = new TreeNode(8);
46     root.right.right.right = new TreeNode(9);
47     ArrayList[] result = verticalOrderTraveralBT(root);
48     for(int i = 0; i < max - min + 1; i ++){
49         System.out.println(result[i].toString());
50     }
51 }

 

转载于:https://www.cnblogs.com/reynold-lei/p/4338665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值