• Lc102_二叉树的层序遍历


    package com.example.demo;
    
    import org.springframework.util.CollectionUtils;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Queue;
    
    /**
     * 102. 二叉树的层序遍历
     * 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
     * <p>
     * <p>
     * <p>
     * 示例:
     * 二叉树:[3,9,20,null,null,15,7],
     * <p>
     * 3
     * / 
     * 9  20
     * /  
     * 15   7
     * 返回其层次遍历结果:
     * <p>
     * [
     * [3],
     * [9,20],
     * [15,7]
     * ]
     * 通过次数144,064提交次数229,041
     */
    public class Lc102 {
    
        public static void main(String[] args) {
            Integer[] arr = new Integer[]{1, 2, 3, 4, 5, 6, 7};
            TreeNode root = CreateNode.createTree(arr).get(0);
            List<List<Integer>> lists = levelOrder(root);
            lists.forEach(n -> {
                n.forEach(m -> {
                    System.out.print(m + ",");
                });
                System.out.println();
            });
        }
    
        public static List<List<Integer>> levelOrder(TreeNode root) {
    
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) {
                return res;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            TreeNode curr = root;
            queue.add(curr);
            while (!queue.isEmpty()) {
                List<Integer> list = new ArrayList<>();
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    list.add(queue.peek().val);
                    curr = queue.poll();
                    if (curr.left != null) {
                        queue.add(curr.left);
                    }
                    if (curr.right != null) {
                        queue.add(curr.right);
                    }
                }
                if (!CollectionUtils.isEmpty(list)) {
                    res.add(list);
                }
            }
            return res;
        }
    }
    
    
  • 相关阅读:
    [poj] 3068 "Shortest" pair of paths || 最小费用最大流
    [poj] 3686 The Windy's || 最小费用最大流
    [poj] 1235 Farm Tour || 最小费用最大流
    [poj] 3281 Dining || 最大流
    [poj] 3041 Asteroids || 最小点覆盖=最大二分图匹配
    luogu P1072 Hankson 的趣味题
    二分图最佳匹配
    181106 solution
    luogu P2216 [HAOI2007]理想的正方形
    luogu P4362 [NOI2002]贪吃的九头龙
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/13130392.html
Copyright © 2020-2023  润新知