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

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);
    }

}

}

全部评论

相关推荐

小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
下个早班:秒挂就是不缺人
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
07-07 11:33
江南大学 Java
已经在暑假实习了&nbsp;,没有明确说有hc,纠结实习到八月份会不会有点影响秋招毕竟感觉今年好多提前批
程序员小白条:92的话准备提前批,其他没必要,没面试机会的,而且你要准备充分,尤其八股和算法题
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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