Sword refers to Offer 32 - I II III. Print binary tree from top to bottom

Print the binary tree layer by layer from top to bottom, the nodes of the same layer are printed in order from left to right, and each layer is printed to one line.

level traversal

answer

The first thing that should come to mind for hierarchical traversal is to implement it with the help of queues.

Why do you need to use Queue to solve layer order traversal?
Queue feature: first in first out
tree feature: only the upper layer can be obtained to get the next layer
Solution: a Node comes out of the queue, and its left and right children are added to the Queue

  1. Need to use two data structures Queue (implemented by LinkedList) and List (implemented by ArrayList)
  2. The role of Queue is to put the left and right children into the queue after passing the empty judgment according to the node out of the queue
  3. The role of List is to save the value of the node out of the queue
  4. Pay attention to converting List to array

Sword refers to Offer 32 - I. Print binary tree from top to bottom

class Solution {
    
    
    // 利用 queue 的特性来进行层序遍历
    Queue<TreeNode> queue = new LinkedList<>();
    // 利用 list 来用于保存遍历的数据
    List<Integer> list = new ArrayList<>();

    public int[] levelOrder(TreeNode root) {
    
    
        int[] res = new int[0];
        if(root==null){
    
    
            return res;
        }
        // 核心方案:queue中取出一个元素,再把其左右孩子加入 queue
        queue.add(root);

        while (!queue.isEmpty()){
    
    
            TreeNode node = queue.remove();
            list.add(node.val);
            if(node.left!=null){
    
    
                queue.add(node.left);
            }
            if(node.right!=null){
    
    
                queue.add(node.right);
            }
        }
        // 把 ArrayList 类型转换为 int[] 数组
        res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
    
    
            res[i] = list.get(i);
        }
        return res;
    }
}

answer

Each layer is put into a List, and each layer is distinguished by for loop queue.size()

class Solution {
    
    
    Queue<TreeNode> queue = new LinkedList<>();
    List<List<Integer>> ret = new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
    
    
        if(root==null){
    
    
            return ret;
        }
        queue.add(root);

        while(!queue.isEmpty()){
    
    
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            for(int i=0;i<size;i++){
    
    
                TreeNode node = queue.remove();
                list.add(node.val);
                if(node.left!=null){
    
    
                    queue.add(node.left);
                }
                if(node.right!=null){
    
    
                    queue.add(node.right);
                }
            }
            ret.add(list);
        }
        return ret;
    }
}

answer

It is necessary to introduce a flag bit to indicate whether the currently inserted end plug or head plug is inserted (the insertion subscript needs to be set to 0)

Z type, by tail append -> head insert in the list

class Solution {
    
    
    List<List<Integer>> ret = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
    
    
        if(root==null){
    
    
            return ret;
        }
        queue.add(root);
        Boolean flag = true;
        while(!queue.isEmpty()){
    
    
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            for(int i=0;i<size;i++){
    
    
                TreeNode node = queue.remove();

                if(flag){
    
    
                    list.add(node.val); //奇数行,list尾插追加
                }else{
    
    
                    list.add(0,node.val);// 偶数行,list中头部插入
                }
                if(node.left!=null){
    
    
                    queue.add(node.left);
                }
                if(node.right!=null){
    
    
                    queue.add(node.right);
                }
            }
            flag = !flag;
            ret.add(list);
        }
        return ret;
    }
}

Guess you like

Origin blog.csdn.net/qq_39537400/article/details/123957188