按层遍历二叉树

解法1:

  • 如何确定换行?
  • 标识出 当前层和下一层的分界位置
  • 将队列中的值按层一次一次遍历;即:一次性将当前队列中的当前层的元素遍历完,再在一次性遍历下一层队列元素;
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;

public class Solution {
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();

        if (root != null) {
            queue.offer(root);
            while (!queue.isEmpty()) {
                ArrayList<Integer> levelList = new ArrayList<>();
                int length = queue.size();  // - 区分:标识当前层和下一层的分界位置
                for (int i = 0; i < length; i++) {
                    TreeNode curr = queue.poll();
                    if (curr.left != null) {
                        queue.offer(curr.left);
                    }
                    if (curr.right != null) {
                        queue.offer(curr.right);
                    }
                    levelList.add(curr.val);
                }
                if (!levelList.isEmpty()) {
                    result.add(levelList);
                }
            }
        }
        return result;
    }
}

解法二:

  • 如何确定换行?
  • 记录下"当前行的最右节点last" 和 "下一行的最右节点nlast"
  • 换行时刻:当前节点==last,然后更新last: last = nlast
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;


public class Solution {
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();

        TreeNode last = root;
        TreeNode nlast = null; 
        if (root != null) {
            queue.offer(root);
            //保存第一层
            ArrayList<Integer> firstLevel = new ArrayList<>();
            firstLevel.add(root.val);
            result.add(firstLevel);
            ArrayList<Integer> levelList = new ArrayList<>();
            while (!queue.isEmpty()) {
                TreeNode curr = queue.poll();
                if (curr.left != null) {
                    queue.offer(curr.left);
                    nlast = curr.left;
                    levelList.add(curr.left.val);
                }
                if (curr.right != null) {
                    queue.offer(curr.right);
                    nlast = curr.right;
                    levelList.add(curr.right.val);
                }
                //换行时刻
                if(curr == last) { 
                    last = nlast;
                    if (!levelList.isEmpty()) {
                        result.add(levelList);
                        levelList = new ArrayList<>(); //记得清空数组
                    }
                }
            }
        }
        return result;
    }
}