求助 1,4 Runtime Error
查看原帖
求助 1,4 Runtime Error
346950
coder_001楼主2020/6/29 12:00

求助 case 1,4 Runtime Error

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {

	static int N,D;
	static Node[] btree = new Node[1000001];
	
	static class Node{
		int l;
		int r;
		
		public Node(int l, int r){
			this.l = l;
			this.r = r;
		}
		
		@Override
		public String toString(){
			return "[" + this.l + "," + this.r + "]";
		}
	}
	
	static void dfs(int i, int deepth){
		
		if (i==0){
			return;
		}
		
		Node nd = btree[i];
		/*
		if(nd.l == 0 && nd.r == 0){
			return;
		}
		*/
		deepth++;
		if (deepth > D){
			D = deepth;
		}
		
		if(nd.l != 0){
			dfs(nd.l,deepth);
		}
		
		if(nd.r != 0){
			dfs(nd.r,deepth);
		}
	}
	
	public static void main(String[] args) throws Exception{
		
		//System.setIn(new FileInputStream("src/dfs/P4913/input.txt"));
		StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		st.nextToken();
		
		N = (int)st.nval;
		
		for (int i=1; i<=N; i++){
			st.nextToken();
			int l = (int)st.nval;
			st.nextToken();
			int r = (int)st.nval;
			btree[i] = new Node(l,r);
		}
		D = 0;
		dfs(1,0);
		
		System.out.println(D);
		
	}

}
2020/6/29 12:00
加载中...