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

102.二叉树的层序遍历

作者:互联网

层序遍历

class Solution {
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        checkFun(root);
        return result;
    }
    public void checkFun(TreeNode node) {
        if (node == null) return;
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(node);
        while (!que.isEmpty()) {
            List<Integer> List = new ArrayList<>();
            int len = que.size();
            while (len > 0) {
                TreeNode tmpNode = que.poll();
                List.add(tmpNode.val);
                if (tmpNode.left != null) que.offer(tmpNode.left);
                if (tmpNode.right != null) que.offer(tmpNode.right);
                len--;
            }
            result.add(List);
        }
    }
}

标签:node,层序,List,que,二叉树,102,new,null,tmpNode
来源: https://blog.csdn.net/Split_token/article/details/122036753