Skip to content

Latest commit

 

History

History
53 lines (43 loc) · 1.31 KB

binary-tree-level-order-traversal.md

File metadata and controls

53 lines (43 loc) · 1.31 KB
title date tags
102. 二叉树的层序遍历
2025-01-21
二叉树

问题描述

题目链接

一层层地遍历二叉树。

解题思路

方法一

要求将每一层的元素作为一个集合,每次根据当前队列中元素的数量(即当前层节点的数量)遍历该层的所有节点,并将其下一层的所有节点添加到队列中。

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int n = queue.size();
            List<Integer> tmp = new ArrayList<>();
            while (n-- > 0) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            ans.add(tmp);
        }
        return ans;
    }
}

算法复杂度分析:

时间复杂度:$O(n)$

空间复杂度:$O(n)$