根节点到最小的叶子节点的路径

import java.util.*; public class Main {

static List<Integer> pList = new ArrayList<>();
public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    while(in.hasNextInt()){
        pList.clear();
        int n = in.nextInt();
        int[] arr = new int[n];
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            arr[i] = in.nextInt();
        }
        dfs(0, list, arr);
        for(int p:pList){
            System.out.print(p + " ");
        }
    }
}

public static void dfs(int i, ArrayList<Integer> list, int[] arr){
    if(!list.isEmpty() && list.get(list.size()-1)==-1) return;
    if(i >= arr.length){
        if(pList.isEmpty()||pList.get(pList.size()-1)>list.get(list.size()-1)){
            pList.addAll(list);
        }
    } else {
        list.add(arr[i]);
        dfs(2*(i+1)-1, list, arr);
        dfs(2*(i+1), list, arr);
        list.remove(list.size()-1);
    }

}

}

全部评论

相关推荐

昨天 17:42
酷酷的喜马拉雅山:你为什么发我的offer列表?
点赞 评论 收藏
分享
赛博小保安:你这简历没啥大问题的,经历技能也足够了,问题应该就是出在出身了,学院本就是这样,HR忙着跟92的勾搭呢,哪有心思看我们这些双非😿😭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务