java - 树的创建及最高度

      前言:好吧 刚刚在牛客网上被自己和题目坑了一波 内心千万只草泥马在奔跑者 花费了我整整一个晚上的时间在找bug,话不多说我先上题目先。


题目描述

现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度

输入描述:

输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,
下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号

输出描述:

输出树的高度,为一个整数

示例1

输入

5
0 1
0 2
1 3
1 4

输出

3
  • 思考题目
  • 思考出我们所需要的数据结构
  • 编程

好吧 我下面直接就上代码吧

(提醒:我这里坑就是题目是建出二叉树,我直接就是做成一棵正常的树了所以在牛客网的用例六检测错误)

 

import java.awt.List;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	
	static int book[] = new int[1003];
	
	static int result = 1;
    
	static int depth = 0;
	
	static Node head = null;
	
	public static ArrayList<Node> list = new ArrayList<Node>();
	
	static class Node{
		int data;
		Node father = null;
		ArrayList<Node> son = new ArrayList<Node>();
		public Node(int data) {
			this.data = data;
		}
	}
	
	public void dfs(Node node) {
		for(int i = 0 ;i< node.son.size();i++) {
			if(node.son.get(i)!=null&&book[node.son.get(i).data]==0) {
				book[node.son.get(i).data]=1;
				result++;
				if(result>depth) {
					depth = result;
				}
				if(node.son.get(i).son==null) {
					return;
				}
				dfs(node.son.get(i));
				result--;
			}
		}
		return;
	}
	
	public static void main(String[] arg) {
		Scanner scanner = new Scanner(System.in);
		int number = scanner.nextInt();
		for(int i = 0;i <number-1;i++) {
			int dataf = scanner.nextInt();
			int self = scanner.nextInt();
			
			int temp30 = 0,temp40 = 0;
			int res = 0;
			for(int m = 0;m<list.size();m++) {
				if(list.get(m).data == dataf) {
					temp30 = m;
					res++;
				}
				if(list.get(m).data == self) {
					temp40 = m;
					res++;
				}
			}
			if(res==2) {
				list.get(temp30).son.clear();
				list.get(temp30).son.add(list.get(temp40));
				list.get(temp30).father=null;
				list.get(temp40).father = list.get(temp30);
				continue;
			}
			if(list.size()==0) {
				Node node1 = new Node(dataf);
				list.add(node1);
				Node node2 = new Node(self);
				list.add(node2);
				node1.son.add(node2);
				node1.father = null;
				node2.father = node1;
			}else {
				int flag = 0;
				int size = list.size();
				for(int k = 0;k<size;k++) {
					if(list.get(k).data==dataf) {
						Node temp  = new Node(self);
						list.get(k).son.add(temp);
						temp.father = list.get(k);
						temp.son = new ArrayList<Node>();
						list.add(temp);
						flag = 1;
						break;
					}
					else if(list.get(k).data == self) {
						Node temp  = new Node(dataf);
						list.add(temp);
						list.get(k).father = temp;
						temp.father = null;
						temp.son.add(list.get(k));
						flag = 1;
						break;
					}
				}
				if(flag!=1){
					Node self1  = new Node(self);
					Node dataf1  = new Node(dataf);
					self1.son = null;
					self1.father = dataf1;
					dataf1.son.add(self1);
					dataf1.father = null;
					list.add(self1);
					list.add(dataf1);
					flag = 0;
				}
			}	
		}
		scanner.close();
		
		for(int i = 0;i<list.size();i++) {
			if(list.get(i).father == null) {
				head = list.get(i);
			}
		}
		Main test3 = new Main();
		test3.dfs(head);
		System.out.println(depth);	
	}
}

我是通过一个list数组来保存每一个节点的信息 ,然后自己封装一个节点的基本信息,其含有指向父节点还有子节点的。然后在对树进行遍历找出最大的高度。


好啦 今天就到此结束 欢迎大家的点评 再见


猜你喜欢

转载自blog.csdn.net/weixin_39460667/article/details/81233477