package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * You need to find the largest value in each row of a binary tree.

 Example:
 Input:

      1
     / \
    3   2
   / \   \
  5   3   9

 Output: [1, 3, 9]
 */
public class _515 {

    public List<Integer> largestValues(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) {
            queue.offer(root);
            while (!queue.isEmpty()) {
                int max = Integer.MIN_VALUE;
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode curr = queue.poll();
                    max = Math.max(max, curr.val);
                    if (curr.left != null) queue.offer(curr.left);
                    if (curr.right != null) queue.offer(curr.right);
                }
                list.add(max);
            }
        }
        return list;
    }
}

@@ -1096,10 +1096,580 @@
 ;%0A    %7D%0A
+%0A    public List%3CInteger%3E largestValuesDFS(TreeNode root) %7B%0A        List%3CInteger%3E res =  new ArrayList%3C%3E();%0A        if(root == null) %7B%0A            return  res;%0A        %7D%0A        dfs(root, res, 0);%0A        return res;%0A%0A    %7D%0A%0A    public void dfs(TreeNode root, List%3CInteger%3E res, int level) %7B%0A        if(root == null) %7B%0A            return;%0A        %7D%0A        if(level == res.size()) %7B%0A            res.add(root.val);%0A        %7D%0A        res.set(level, Math.max(res.get(level), root.val));%0A        dfs(root.left, res, level +1);%0A        dfs(root.right, res, level +1);%0A    %7D%0A%0A
 %7D%0A

add DFS solution for 515