Binary tree to prove safety offer 60. print a plurality of rows

Title Description

Printing the binary tree in layers from top to bottom, from left to right with the output layer node. Each line of output layer.
 

Problem-solving ideas

按层次输出二叉树

 

  * 访问根节点,并将根节点入队。

 

  * 当队列不空的时候,重复以下操作。

 

  * 1、弹出一个元素。作为当前的根节点。

 

  * 2、如果根节点有左孩子,访问左孩子,并将左孩子入队。

 

  * 3、如果根节点有右孩子,访问右孩子,并将右孩子入队。
 

code show as below

  public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        Solution8 solution8 = new Solution8();
        TreeNode treeNode = solution8.createBinaryTreeByArray(array, 0);
        for (ArrayList list :
                solution8.Print(treeNode)) {
            System.out.println(list);
        }
    }
    /**
     * 层次遍历
     *
     * [@param pRoot 根节点
     * @return](/profile/547241) arrayLists
     */
    ArrayList> The Print (the TreeNode PROOT) {
         // store the results 
        the ArrayList> = ArrayLists new new the ArrayList ();
         IF (PROOT == null ) {
             return ArrayLists; 
        } 
        // use the queue, the FIFO 
        Queue Queue = new new the LinkedList ();
         // storing a list of each row of 
        the ArrayList the arrayList = new new the ArrayList ();
         // record the number of present printed layer 
        int Start = 0 ;
         // record play several lower 
        int End =. 1 ; 
        queue.add (PROOT); 
        the while (!queue.isEmpty ()) { 
            the TreeNode TEMP = queue.remove ();
             // add to the Bank of the arrayList 
            arrayList.add (temp.val); 
            Start ++ ;
             // every print a node, the node routes the one layer around the nodes queued, and the number of records to be printed next layer 
            IF (temp.left =! null ) { 
                queue.add (temp.left); 
            } 
            IF (! temp.right = null ) { 
                queue. the Add (temp.right); 
            } 
            // judgment of whether printing is completed layer 
            iF (Start == end) {
                 // at this point the queue is stored in the next layer of nodes, the size of the queue is the end
                end = queue.size();
                start = 0;
                //把arrayList添加到结果列表arrayLists中
                arrayLists.add(arrayList);
                //重置arrayList
                arrayList = new ArrayList();
            }
        }
        return arrayLists;
    }
    private TreeNode createBinaryTreeByArray(int[] array, int index) {
        TreeNode tn = null;
        if (index < array.length) {
            int value = array[index];
            tn = new TreeNode(value);
            tn.left = createBinaryTreeByArray(array, 2 * index + 1);
            tn.right = createBinaryTreeByArray(array, 2 * index + 2);
            return tn;
        }
        return tn;
    }
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }

 

Great God Code

ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();
        depth(pRoot, 1, list);
        return list;
    }
     
    private void depth(TreeNode root, int depth, ArrayList<ArrayList<Integer>> list) {
        if(root == null) return;
        if(depth > list.size())
            list.add(new ArrayList<Integer>());
        list.get(depth -1).add(root.val);
         
        depth(root.left, depth + 1, list);
        depth(root.right, depth + 1, list);
    }

 

Guess you like

Origin www.cnblogs.com/Transkai/p/11416783.html