按之字形顺序打印二叉树Java实现

题目描述

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

分析

用一个布尔量控制从左到右还是从右到左的顺序;如果为真则他的左右孩子按照从左到右的顺序放入list中,否则按照从右到左的顺序,当本行结束之后,再把list中的数据放入一个result中。其中用一个null值作为层次的分隔符。

代码

 

 1 package com.leetcode.printTreeNode;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 import java.util.LinkedList;
 6 
 7 /**
 8  * Created by Feng on 2016/11/8.
 9  * 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,
10  * 第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
11  */
12 public class PrintTreeNode {
13     public static void main(String[] args) {
14         TreeNode node1 = new TreeNode(1);
15         TreeNode node2 = new TreeNode(2);
16         TreeNode node3 = new TreeNode(3);
17         TreeNode node4 = new TreeNode(4);
18         TreeNode node5 = new TreeNode(5);
19         TreeNode node6 = new TreeNode(6);
20         TreeNode node7 = new TreeNode(7);
21         TreeNode node8 = new TreeNode(8);
22         TreeNode node9 = new TreeNode(9);
23         node1.left = node2;
24         node1.right = node3;
25         node2.left = node4;
26         node2.right = node5;
27         node3.left = node6;
28         node3.right = node7;
29         node4.left = node8;
30         node4.right = node9;
31         ArrayList<ArrayList<Integer>> result = printTreeNode(node1);
32         for (ArrayList list : result) {
33             for (int i = 0; i < list.size(); i++) {
34                 System.out.print(list.get(i) + " ");
35             }
36         }
37     }
38 
39     public static ArrayList<ArrayList<Integer>> printTreeNode(TreeNode pRoot) {
40 
41         ArrayList<ArrayList<Integer>> result = new ArrayList<>();
42         if (pRoot == null) {
43             return result;
44         }
45 
46         ArrayList<Integer> list = new ArrayList<>();
47         LinkedList<TreeNode> queue = new LinkedList<>();
48         queue.addLast(null);//层分隔符
49         queue.addLast(pRoot);
50         boolean leftToRight = true;
51         while (queue.size() != 1) {
52             TreeNode node = queue.removeFirst();
53             if (node == null) {//到达层分隔符
54                 Iterator<TreeNode> iter = null;
55                 if (leftToRight) {
56                     iter = queue.iterator();//从前往后遍历
57                 } else {
58                     iter = queue.descendingIterator();//从后往前遍历
59                 }
60                 leftToRight = !leftToRight;
61                 while (iter.hasNext()) {
62                     TreeNode temp = (TreeNode) iter.next();
63                     list.add(temp.val);
64                 }
65                 result.add(new ArrayList<Integer>(list));
66                 list.clear();
67                 queue.addLast(null);//添加层分隔符
68                 continue;//一定要continue
69             }
70             if (node.left != null) {
71                 queue.addLast(node.left);
72             }
73             if (node.right != null) {
74                 queue.addLast(node.right);
75             }
76         }
77 
78         return result;
79     }
80 
81 }
82 
83 class TreeNode {
84     int val = 0;
85     TreeNode left = null;
86     TreeNode right = null;
87 
88     public TreeNode(int val) {
89         this.val = val;
90     }
91 }

运行结果

 

1 3 2 4 5 6 7 9 8 
Process finished with exit code 0

 

posted @ 2016-11-08 20:32  凌风1205  阅读(2203)  评论(0编辑  收藏  举报
//一下两个链接最好自己保存下来,再上传到自己的博客园的“文件”选项中