GithubHelp home page GithubHelp logo

blogs's Introduction

blog

来写点什么

blogs's People

Contributors

moringl avatar

Watchers

 avatar

blogs's Issues

二叉树的先序、中序、后序、层序遍历,使用递归以及非递归方式实现

一、先序遍历

递归

public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> ret = new LinkedList<>();
    recur(root, ret);
    return ret;
}

private void recur(TreeNode node, List<Integer> values) {
    if(node == null) return;
    values.add(node.val);
    recur(node.left, values);
    recur(node.right, values);
}

非递归

public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> ret = new ArrayList<>();
    Stack<TreeNode> stack =  new Stack<>();
    TreeNode node = root;
    while(!stack.empty() || node != null) {
        while(node != null) {
            ret.add(node.val);
            stack.push(node);
            node = node.left;
        }
        node = stack.pop().right;
    }
    return ret;
}

二、中序遍历

递归

public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> ret = new LinkedList<>();
    recur(root, ret);
    return ret;
}

private void recur(TreeNode node, List<Integer> values) {
    if(node == null) return;
    recur(node.left, values);
    values.add(node.val);
    recur(node.right, values);
}

非递归

public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> ret = new ArrayList<>();
    Stack<TreeNode> stack = new Stack<>();
    while(!stack.empty() || root != null) {
        while(root != null) {
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        ret.add(root.val);
        root = root.right;
    }
    return ret;
}

三、后序遍历

递归

public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> ret = new LinkedList<>();
    recur(root, ret);
    return ret;
}

private void recur(TreeNode node, List<Integer> values) {
    if(node == null) return;
    recur(node.left, values);
    recur(node.right, values);
    values.add(node.val);
}

非递归

public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    Deque<TreeNode> stack = new LinkedList<TreeNode>();
    TreeNode prev = null;
    while (root != null || !stack.isEmpty()) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        if (root.right == null || root.right == prev) {
            res.add(root.val);
            prev = root;
            root = null;
        } else {
            stack.push(root);
            root = root.right;
        }
    }
    return res;
}

四、层序遍历

递归

public List<List<Integer>> levelOrder(TreeNode root) {
    if (root == null) {
        return new ArrayList<List<Integer>>();
    }
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    dfs(1, root, res);
    return res;
}

private void dfs(int index, TreeNode root, List<List<Integer>> res) {
    if (res.size() < index) {
        res.add(new ArrayList<Integer>());
    }
    res.get(index - 1).add(root.val);
    if (root.left != null) {
        dfs(index + 1, root.left, res);
    }
    if (root.right != null) {
        dfs(index + 1, root.right, res);
    }
}

非递归

public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> ret = new ArrayList<>();
    LinkedList<TreeNode> nodes =  new LinkedList<>();
    if(root != null) nodes.add(root);
    while(!nodes.isEmpty()) {
        int size = nodes.size();
        List<Integer> sub = new ArrayList<>();
        for(int i = 0; i < size; i++) {
            TreeNode node = nodes.removeFirst();
            sub.add(node.val);
            if(node.left != null) nodes.add(node.left);
            if(node.right != null) nodes.add(node.right);
        }
        ret.add(sub);
    }
    return ret;
}

开篇,写点什么

又开始想搭博客了。不知道写啥,先写着呗

兜兜转转,失去了最初始写博客的本心,最开始想简单记一点东西,后面却花了大量的时间在探索其他方面。


去过CSDN,写过几十篇文章,但是CSDN各种审核和下载积分让人难受,一些文章也莫名的被封,虽然找客服后有解封,但是有点厌烦这种束缚的感觉,后面也逐渐不写了,改成了本地记录。

逐渐弃用CSDN期间,一边在用语雀+插件记录文章素材,用typora+图床写博文,也攒了些文章下来,自己胡乱写写,但也想分享出去。
另一边自己买了服务器和域名,写前端写后端,配置服务器,搭建了自己的博客站点,有点成就感,但属实费劲,不大得劲的后台管理系统,和略丑的前端页面,还有等待续费的服务器,后续也舍弃了这种方式。

本着想分享一些自己写的博文,又萌生出搭建博客的想法。
于是开始整活,用Hexo和github page搭建出来了。写一篇文章到页面,需要利用hexo原生的一系列命令,费劲。有个hexo-admin的插件,安装过,不好用,个人觉着也不好看,遂弃之,放弃了这整套方案。

诚然,最近又想在线上写博客了,方便随时查看检索,也可以分享给志同道合的朋友。也无意间,或是突然开窍,发现issues就是个天然的blog平台,可以使用markdown,还有评论,还可以贴小标签 (●'◡'●)
硬伤的地方,网速有点问题,除此之外,倒也还好。也去了解了一下,也有不少大佬是用issue写博客,哈哈哈,是有点开心的,我不是一个人。

在此开篇,随便写点什么,开心就好 ^_^

Win 系统设置cmd窗口utf-8编码格式

cmd窗口执行 chcp 命令可以查看当前编码格式。win上默认是GBK格式,所以执行显示对应的编码936
image

命令 chcp 56001 可以临时修改当前窗口的编码格式为 utf-8。


一劳永逸的办法,需要修改注册表。Win + R 搜索 regedit 打开注册表编辑器

  1. 按路径 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor 打开文件夹
  2. 新建字符串值,命名 Autorun
  3. 修改数值数据为 chcp 65001 && cls
    image

配置完成,以后打开cmd时就都是utf-8的编码格式

chcp 65001命令已经足够修改编码了,但是每次打开cmd窗口时会显示chcp命令,所以再增加了一条 && cls 清屏指令

git bash中文乱码

  1. 右键bash窗口或标题栏,选择选项,打开如下所示
    image

  2. 设置本地字符集为zh_CN,以及编码格式为utf-8

  3. 若路径等还是乱码,执行如下命令

git config --global core.quotepath false
  1. 若还是乱码,可能是bash本身编码不对,可以按照 #4 将窗口本身设置为utf-8的格式

一套组合拳下来,自身的窗口是解决了乱码问题

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.