/**
* 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;
}
}