Iterative Java

  • 0
    class Solution {
        public List<Integer> largestValues(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            if (root == null) return res;
            Queue<TreeNode> queue = new LinkedList<>();
            while (!queue.isEmpty()){
                int size = queue.size();
                int max = Integer.MIN_VALUE;
                for(int i=0;i<size;i++){
                    TreeNode head = queue.poll();
                    if (max < head.val) max = head.val;
                    if (head.left != null) queue.offer(head.left);
                    if (head.right != null) queue.offer(head.right);
            return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.