ARTS打卡第十四周

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值