【剑指Offer】61、把二叉树打印成多行

题目描述

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

题解一:BFS
 1 public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<>();
 3         LinkedList<TreeNode> queue = new LinkedList<>();
 4         if(pRoot==null){
 5             return res;
 6         }
 7         queue.offer(pRoot);
 8         while (queue.size()!=0){
 9             int size=queue.size();
10             ArrayList<Integer> list = new ArrayList<>();
11             for(int i=0;i<size;i++) {
12                 TreeNode node = queue.poll();
13                 if(node==null){
14                     continue;
15                 }
16                 list.add(node.val);
17                 queue.offer(node.left);
18                 queue.offer(node.right);
19             }
20             if(list.size()!=0){
21                 res.add(list);
22             }
23         }
24         return res;
25     }
题解二:递归
 1 public static ArrayList<ArrayList<Integer>> Print01(TreeNode pRoot) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<>();
 3         dept(pRoot,1,res);
 4         return res;
 5     }
 6     public static void dept(TreeNode root,int depth,ArrayList<ArrayList<Integer>> res){
 7         if(root==null){
 8             return;
 9         }
10         //判断是否进入到下一层
11         if(depth>res.size()){
12             res.add(new ArrayList<Integer>());
13         }
14         //树的每一层都是一个list,将节点的值放入新创建的一层的list中
15         res.get(depth-1).add(root.val);
16         dept(root.left,depth+1,res);
17         dept(root.right,depth+1,res);
18     }

初始化树:

 1 public static class TreeNode{
 2         int val=0;
 3         TreeNode left=null;
 4         TreeNode right=null;
 5         public TreeNode(int val){
 6             this.val=val;
 7         }
 8     }
 9  private static List<TreeNode> nodeList = null;
10     public static TreeNode createBinTree(int[] array) {
11         nodeList=new LinkedList<TreeNode>();
12         // 将一个数组的值依次转换为TreeNode节点
13         for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
14             nodeList.add(new TreeNode(array[nodeIndex]));
15         }
16         // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
17         for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
18             // 左孩子
19             nodeList.get(parentIndex).left = nodeList
20                     .get(parentIndex * 2 + 1);
21             // 右孩子
22             nodeList.get(parentIndex).right = nodeList
23                     .get(parentIndex * 2 + 2);
24         }
25         // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
26         int lastParentIndex = array.length / 2 - 1;
27         // 左孩子
28         nodeList.get(lastParentIndex).left = nodeList
29                 .get(lastParentIndex * 2 + 1);
30         // 右孩子,如果数组的长度为奇数才建立右孩子
31         if (array.length % 2 == 1) {
32             nodeList.get(lastParentIndex).right = nodeList
33                     .get(lastParentIndex * 2 + 2);
34         }
35         return nodeList.get(0);
36     }

测试:

 1 public static void main(String[] args) {
 2         int[] tree={8,6,10,5,7,9,11};
 3         TreeNode rootNode = createBinTree(tree);
 4         ArrayList<ArrayList<Integer>> lists = Print(rootNode);
 5         for (ArrayList<Integer> list : lists) {
 6             System.out.println(list);
 7         }
 8     }
 9 输出:
10 [8]
11 [6, 10]
12 [5, 7, 9, 11]

猜你喜欢

转载自www.cnblogs.com/Blog-cpc/p/12356760.html