其他分享
首页 > 其他分享> > 二叉树层序遍历

二叉树层序遍历

作者:互联网

采用广度优先遍历(Breath First Search),使用队列实现,一般模板如下:

102. Binary Tree Level Order Traversal

//迭代,借助队列实现
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> temp = new ArrayList<>();
            for (int i  = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                temp.add(cur.val);
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
            res.add(temp);
        }
        return res;
    }
}

 

也可以使用递归:

//递归
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        order(root, 0, res);
        return res;
    }
    
    void order(TreeNode node, int deep, List<List<Integer>> res) {
        if (node == null) return;
        deep++;
        //层级界定
        if (res.size() < deep) {
            List<Integer> temp = new ArrayList<>();
            res.add(temp);
        }
        res.get(deep - 1).add(node.val);
        order(node.left, deep, res);
        order(node.right, deep, res);
    }
}

 

既然明白了基本原理,接下来就是打十个

107. 二叉树的层序遍历 II

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> temp = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                temp.add(cur.val);
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
            res.add(temp);
        }
        Collections.reverse(res);
        return res;
    }
}

199. 二叉树的右视图

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
                //该层最右
                if (i == size - 1) res.add(cur.val);
            }
        }
        return res;
    }
}

637. 二叉树的层平均值

class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            double levelSum  = 0.0;
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
                levelSum += cur.val;
            }
            res.add(levelSum / size);
        }
        return res;
    }
}

429. N 叉树的层序遍历

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<Node> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node cur = queue.poll();
                for (Node child : cur.children) {
                    if (child != null) queue.offer(child);
                }
                temp.add(cur.val);
            }
            res.add(temp);
        }
        return res;
    }
}

515. 在每个树行中找最大值

class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                max = max > cur.val ? max : cur.val;
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
            res.add(max);
        }
        return res;
    }
}

116. 填充每个节点的下一个右侧节点指针

class Solution {
    public Node connect(Node root) {
        Deque<Node> queue = new LinkedList<>();
        if (root != null) queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            Node pre = queue.peek(); //前驱结点
            for (int i = 0; i < size; i++) {
                Node cur = queue.poll();
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
                //非该层一个节点,将当前节点赋值给前驱节点的后继。
                if (i != 0) {
                    pre.next = cur;
                    pre = cur;
                }
                //next默认值null
                //if (i == size - 1) cur.next = null;
            }
        }
        return root;
    }
}

117. 填充每个节点的下一个右侧节点指针 II

 代码与上同

留两个明天继续打。

参考:代码随想录:programmercarl.com

标签:遍历,cur,res,层序,queue,二叉树,null,root,size
来源: https://www.cnblogs.com/lizihhh/p/leetcode_binary_tree2.html