437.路径总和III | 297.二叉树的序列化和反序列化 | 239.滑动窗口最大值 |

/**
 * 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 {
    //pathSum 计算任意节点路径树;countNode计算某个特定节点开始的路径和
    private int count=0;
    public int pathSum(TreeNode root, int targetSum) {
        //终止条件
        if(root == null){
            return 0;
        }
        //计算路径和
        countNode(root,targetSum);
        //递归
        pathSum(root.left,targetSum);
        pathSum(root.right,targetSum);
        return count;
    }
    private void countNode(TreeNode root, int targetSum){
        if(root == null){
            return;
        }
        if(root.val == targetSum){
            count++;
        }
        countNode(root.left,targetSum-root.val);
        countNode(root.right,targetSum-root.val);
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        
        //前序遍历得到List
        StringBuilder string = new StringBuilder();
        sb(string,root);
        return string.toString();
    }
    private void sb(StringBuilder string,TreeNode root){
        if(root == null){
            string.append("null").append(",");
            return;
        }
        string.append(root.val).append(",");
        sb(string,root.left);
        sb(string,root.right);
    } 

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {

        String[] temp = data.split(",");
        Queue<String> que = new LinkedList<>(Arrays.asList(temp));
        
        return deserial(que);
    }
    private TreeNode deserial(Queue<String> que){
        String val = que.poll();
        if(val.equals("null")){
            return null;
        }

        TreeNode root = new TreeNode(Integer.parseInt(val));
        root.left = deserial(que);
        root.right = deserial(que);
        return root;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int[] max = new int[nums.length-k+1];
        Deque<Integer> deq = new LinkedList<>();

        //初始化
        for(int i=0;i<nums.length;i++){
            if(!deq.isEmpty() && deq.peekFirst()<i-k+1){
                deq.pollFirst();
            }
            while(!deq.isEmpty() && nums[deq.peekLast()]<=nums[i]){
                deq.pollLast();
            }
            deq.addLast(i);
            if(i-k+1>=0){
                max[i-k+1]=nums[deq.peekFirst()];
            }
        }
        return max;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值