算法面试题

题目:
        输入规格:
                每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数N(≤10),它是树中节点的总数,节点的编号从0到N-1。 然后跟随N行,每行对应一个从0到N-1的节点,并给出该节点的左,右子节点的索引。 如果孩子不存在,则将-放在该位置。 任何一对孩子都用空格隔开。
        输出规格:
                对于每个测试用例,在第一行中打印层序顺序,然后在第二行中打印倒置树的中序遍历序列。 相邻数字之间必须恰好有一个空格,行尾不得有多余的空格。

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

C++
        一个算法大佬的解答:
                https://www.cnblogs.com/wlw-x/p/11681428.html

JAVA

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

/**
 * 
 * @author 83965
 *
 */
public class ReverseTwoTree {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n;
		System.out.println("请输入树的大小:");
		n=scanner.nextInt();
		TwoTree tT=new TwoTree(n);
		tT.initTree();
		tT.layerPrint(tT.getRoot());
		System.out.println();
		tT.midPrint(tT.getRoot());
	}
}
class TreeNode{
	private int data;
	private TreeNode lchrild;
	private TreeNode rchrild;
	
	
	
	public int getData() {
		return data;
	}

	public void setData(int data) {
		this.data = data;
	}

	public TreeNode getLchrild() {
		return lchrild;
	}

	public void setLchrild(TreeNode lchrild) {
		this.lchrild = lchrild;
	}

	public TreeNode getRchrild() {
		return rchrild;
	}

	public void setRchrild(TreeNode rchrild) {
		this.rchrild = rchrild;
	}

	public TreeNode(int data) {
		super();
		this.data = data;
	}
	
	public TreeNode() {
	}
}

class TwoTree{
	private int mask[];///用于找根结点
	private TreeNode sum[];//所有树的节点
	private TreeNode root;//根节点
//	private HashMap<Integer,HashMap> map;//用来存每个节点的左右孩子
	private HashMap<String,Integer> m=new HashMap<>();
	
	
	public TreeNode getRoot() {
		return root;
	}

	public TwoTree(int n){
		mask=new int[n];
		sum=new TreeNode[n];
		for(int i=0;i<n;i++) {
			sum[i]=new TreeNode(i);
		}
		Scanner scanner=new Scanner(System.in);
		char a,b;
		for(int i=0;i<n;i++) {
			String temp=scanner.nextLine();
			a=temp.charAt(0);
			b=temp.charAt(2);
			if(a!='-') {
				m.put("left"+i,a-'0');
				mask[a-'0']=1;
			}
			else {
				m.put("left"+i,null);
			}
			if(b!='-') {
				m.put("right"+i,b-'0');
				mask[b-'0']=1;
			}
			else {
				m.put("right"+i,null);
			}
		}
		int flag=0;
		for(int i=0;i<n;i++) {
			if(mask[i]==0) {
				flag=i;
				break;
			}
		}
		root=sum[flag];//找到根节点
	}
	
	public void initTree() {//反转着建立树,方便输出答案
		for(int i=0;i<sum.length;i++) {
			if((m.get("right"+i))!=null) {
				sum[i].setLchrild(sum[m.get("right"+i)]);
			}
			else 
				sum[i].setLchrild(null);
			if((m.get("left"+i))!=null) {
				sum[i].setRchrild(sum[m.get("left"+i)]);
			}
			else 
				sum[i].setRchrild(null);
		}
	}
	
	public void foPrint(TreeNode root){//先序遍历
		TreeNode temp=root;
		System.out.print(root.getData()+" ");
		if(temp.getLchrild()!=null)
			foPrint(temp.getLchrild());
		if(temp.getRchrild()!=null)
			foPrint(temp.getRchrild());
	}
	
	public void midPrint(TreeNode root){//中序遍历
		TreeNode temp=root;
		if(temp.getLchrild()!=null)
			midPrint(temp.getLchrild());
		System.out.print(root.getData()+" ");
		if(temp.getRchrild()!=null)
			midPrint(temp.getRchrild());
	}

	public void bhPrint(TreeNode root){//后序遍历
		TreeNode temp=root;
		if(temp.getLchrild()!=null)
			bhPrint(temp.getLchrild());
		System.out.print(root.getData()+" ");
		if(temp.getRchrild()!=null)
			bhPrint(temp.getRchrild());
	}
	
	public void layerPrint(TreeNode root) {//层序遍历
		TreeNode temp=root;
		Queue<TreeNode> que=new LinkedList<>();
		List<Integer> list=new ArrayList<>();
		que.offer(temp);
		TreeNode t;
		while(!que.isEmpty()) {
			t=que.peek();
			que.poll();
			list.add(new Integer(t.getData()));
			if(t.getLchrild()!=null)
				que.offer(t.getLchrild());
			if(t.getRchrild()!=null)
				que.offer(t.getRchrild());
			
		}
		for(int i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值