Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

&nbsp;
Example 1:

Input: root = [1,3,2,5,3,null,9]
Output: [1,3,9]


Example 2:

Input: root = [1,2,3]
Output: [1,3]


&nbsp;
Constraints:


	The number of nodes in the tree will be in the range [0, 104].
	-231 &lt;= Node.val &lt;= 231 - 1


class Solution(object):
    def largestValues(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        ans=[]
        q=[]
        q.append(root)
        while q:
            s=len(q)
            t=[]
            for i in range(s):
                n=q.pop(0)
                t.append(n.val)
                if n.left:
                    q.append(n.left)
                if n.right:
                    q.append(n.right)
            ans.append(max(t))
        return ans
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    private List<Integer> li=new ArrayList<>();

    public List<Integer> largestValues(TreeNode root) {

        if(root==null) return li; //if root is NULL

        //using bfs(level-order)
        Queue<TreeNode> q=new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            int size=q.size();
            int res=Integer.MIN_VALUE;
            while(size-->0){
                TreeNode temp=q.poll();
                if(temp.left!=null) q.add(temp.left);
                if(temp.right!=null) q.add(temp.right);
                res =Math.max(res,temp.val); //comparing every node in each level to get max
            }
            li.add(res); //adding each level Max value to the list
        }
        return li;
    }
}
class Solution {
public:
    vector<int>res;
    vector<int> largestValues(TreeNode* root) {
        if(!root) return {};
        if(!root->left && !root->right) return {root->val};
        TreeNode*temp;
        int mx = INT_MIN;
        queue<TreeNode*>q;
        q.push(root);

        while(!q.empty()){
            int sz = q.size();
            while(sz--){
                temp = q.front();
                if(temp->val > mx) mx = temp->val;
                q.pop();
                if(temp->left) q.push(temp->left);
                if(temp->right) q.push(temp->right);
            }
            res.push_back(mx);
            mx = INT_MIN;
        }
        return res;
    }
};
var largestValues = function(root) {
    if(!root) return [];
    
    const op = [];
    const Q = [[root, 1]];
    while(Q.length) {
        const [r, l] = Q.shift();
        
        if(op.length < l) op.push(-Infinity);
        op[l-1] = Math.max(op[l-1], r.val);
        
        if(r.left) Q.push([r.left, l + 1]);
        if(r.right) Q.push([r.right, l + 1]);
    }
    return op;
};
Find Largest Value in Each Tree Row