Algorithm: Leetcode 94. Binary Tree Inorder Traversal
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
一、递归写法:
public List<Integer> recursive(TreeNode root) {
if(root == null) {
return Collections.emptyList();
}
List<Integer> result = new ArrayList<>();
result.addAll(recursive(root.left));
result.add(root.val);
result.addAll(recursive(root.right));
return result;
}
分析:时间复杂度O(n),空间复杂度O(n)
二、迭代写法:
public List<Integer> iterate(TreeNode root) {
if(root == null) {
return Collections.emptyList();
}
LinkedList<TreeNode> stack = new LinkedList<>();
List<Integer> result = new ArrayList<>();
stack.addLast(root);
while(!stack.isEmpty()) {
TreeNode node = stack.getLast();
if(node.left == null) {
node = stack.removeLast();
result.add(node.val);
if (node.right != null) {
stack.addLast(node.right);
}
} else {
stack.addLast(node.left);
node.left = null;
}
}
return result;
}
分析:时间复杂度O(n),空间复杂度O(n)
Review:
Tip: 使用Jersey上传下载文件,文件名中文乱码的解决办法
在上传文件的接口中,使用FormDataContentDisposition获取到的文件名是乱码,因为Jersey中默认使用ISO_8859_1对文件名解码成字符串,此时这个字符串如果包含中文,看到的就是乱码,需要调用String的getBytes方法重新将其编码成ISO_8859_1的字节数组,然后在用String的构造方法将其解码成UTF_8的字符串。
/**
* 上传报竣截图.
*/
@POST
@Path("/file/upload")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadNotifyComplementAttachment(@Context SecurityContext sc,
@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String fileName = new String(fileDetail.getFileName().getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
// do something
return ResponseUtil.creationSucceed();
}
Share: EBGP vs IBGP
一直搞不清楚IBGP是什么东西,这篇文章说得比较生动,让我有了一个直观的认识。
原文链接:https://zhuanlan.zhihu.com/p/31766603