剑指 Offer:32-II 从上到下打印二叉树 II

86 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第21天,点击查看活动详情

题目

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如: 给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

提示:

  1. 节点总数 <= 1000

注意:本题与主站 102 题相同:leetcode-cn.com/problems/bi…

解题

解题一:广度优先遍历

思路

记录当前层次,以及每一个层的最后一个节点

代码

/**
 * Definition for a binary tree node.
 */
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        // 当前行最后的节点
        TreeNode nowLastNode = root;
        // 下一行最后的节点
        TreeNode nextLastNode = null;
        // 某一行的遍历结果
        List<Integer> tempList = new ArrayList<>();
        // 存放队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            tempList.add(poll.val);
            if (poll.left != null) {
                queue.add(poll.left);
                nextLastNode = poll.left;
            }
            if (poll.right != null) {
                queue.add(poll.right);
                nextLastNode = poll.right;
            }
            if (Objects.equals(poll, nowLastNode)) {
                nowLastNode = nextLastNode;
                result.add(tempList);
                tempList = new ArrayList<>();
            }
        }
        return result;
    }
}

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

总结

性能分析

  • 执行耗时:1 ms,击败了 84.62% 的 Java 用户
  • 内存消耗:41.4 MB,击败了 22.30% 的 Java 用户

解题二:广度优先遍历-优化

思路

去掉 当前行最后节点(nowLastNode) 和 下一行最后节点(nextLastNode),使用队列的长度代替,减少开销

代码

/**
 * Definition for a binary tree node.
 */
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            // 当前队列的数量就是当前行的数量,因此遍历这个数量代表遍历当前行元素
            int num = queue.size();
            List<Integer> tempList  = new ArrayList<>();
            for (int i = 0; i < num; i++) {
                TreeNode poll = queue.poll();
                tempList.add(poll.val);
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
            }
            result.add(tempList);
        }
        return result;
    }
}

总结

性能分析

  • 执行耗时:1 ms,击败了 84.62% 的 Java 用户
  • 内存消耗:41.6 MB,击败了 11.73% 的 Java 用户