Loading

基于Java的二叉树层序遍历打印实现

层序遍历的思路:若树为空,则返回空,否则从树的第一层开始,即从根节点,从上而下逐层遍历。

1. 二叉树层序遍历Ⅰ——剑指offer32-Ⅰ

从上到下,从左到右打印二叉树,返回一维数组int[] res

class Solution {
    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[0];
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        List<Integer> list = new ArrayList<>();
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            list.add(node.val);
            if (node.left != null) q.add(node.left);
            if (node.right != null) q.add(node.right);
        }
        int[] res = new int[list.size()];
        for (int i = 0; i < res.length; i++)
            res[i] = list.get(i);
        return res;
    }
}

2. 二叉树层序遍历Ⅱ——剑指offer32-Ⅱ/LeetCode102

从上到下,从左到右打印二叉树,返回List<List<Integer>> res

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        if (root != null) q.add(root);
        while (!q.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            for (int i = q.size(); i > 0; i--) {
                TreeNode node = q.poll();
                list.add(node.val);
                if (node.left != null) q.add(node.left);
                if (node.right != null) q.add(node.right);
            }
            res.add(list);
        }
        return res;
    }
}

3. 二叉树层序遍历Ⅲ——剑指offer32-Ⅲ/LeetCode103

从上到下,按zigzag方式打印(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行),返回List<List<Integer>> res

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        if (root != null) q.add(root);
        while (!q.isEmpty()) {
            LinkedList<Integer> list = new LinkedList<>();
            for (int i = q.size(); i > 0; i--) {
                TreeNode node = q.poll();
                if (res.size() % 2 == 0) list.addLast(node.val);
                else list.addFirst(node.val);
                if (node.left != null) q.add(node.left);
                if (node.right != null) q.add(node.right);
            }
            res.add(list);
        }
        return res;
    }
}

4. 二叉树层序遍历Ⅳ——LeetCode107

从下到上,从左到右打印二叉树,返回List<List<Integer>> res

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        if (root != null) q.add(root);
        while (!q.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            for (int i = q.size(); i > 0; i--) {
                TreeNode node = q.poll();
                list.add(node.val);
                if (node.left != null) q.add(node.left);
                if (node.right != null) q.add(node.right);
            }
            // 头插法
            res.add(0, list);
        }
        return res;
    }
}
posted @ 2020-08-21 22:38  Chiakiiii  阅读(723)  评论(0编辑  收藏  举报