给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
    示例1:
    image.png

    输入: root = [1,3,2,5,3,null,9]
    输出: [1,3,9]
    示例2:

    输入: root = [1,2,3]
    输出: [1,3]

    提示:

    二叉树的节点个数的范围是 [0,104]
    -231 <= Node.val <= 231 - 1


    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public List<Integer> largestValues(TreeNode root) {
    18. List<Integer> res = new ArrayList<>();
    19. if (root == null) return res;
    20. Deque<TreeNode> q = new LinkedList<>();
    21. q.addLast(root);
    22. while (!q.isEmpty()) {
    23. int size = q.size();
    24. int max = Integer.MIN_VALUE;
    25. while (size -- > 0) {
    26. TreeNode t = q.pollFirst();
    27. max = Math.max(max, t.val);
    28. if (t.left != null) q.addLast(t.left);
    29. if (t.right != null) q.addLast(t.right);
    30. }
    31. res.add(max);
    32. }
    33. return res;
    34. }
    35. }

    lc515. 在每个树行中找最大值(队列) - 图2