386. 字典序排数(多叉树遍历)

 

   分析:十叉树的遍历

class Solution {
    public List<Integer> lexicalOrder(int n) {
        List<Integer> res = new ArrayList<>();
        for(int i = 1; i < 10; i++) {
            dfs(n,res,i);
        }
        return res;
    }
    public void dfs(int n, List<Integer> list, int i) {
        if(i > n) return;
        list.add(i);
        i*=10;
        for(int k = 0; k < 10; k++) {
            dfs(n,list,i+k);
        }
    }
}

 

posted @ 2020-08-25 15:06  Sexyomaru  阅读(119)  评论(0编辑  收藏  举报