秋招每日一题

53 阅读1分钟

image.png

class OrderedStream {

String[] strs;
int k = 0;

public OrderedStream(int n) {
    strs = new String[n];
}

public List<String> insert(int idKey, String value) {
    strs[idKey-1]=value;
    List<String> res = new ArrayList<>();
    while(k<strs.length && strs[k]!=null)
        res.add(strs[k++]);
    return res;
}
}

image.png

/*
* 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 int depth = -1, sum = 0;

private void dfs(TreeNode root, int now_depth){
    if(root == null) return;
    if(now_depth > depth) {depth = now_depth; sum = root.val;}
    else if(now_depth == depth) sum += root.val;

    dfs(root.left, now_depth+1);
    dfs(root.right, now_depth+1);
}

public int deepestLeavesSum(TreeNode root) {
    dfs(root, 0);
    return sum;
}
}