1051: 输出利用先序遍历创建的二叉树中的指定结点的孩子结点java实现

本文探讨了Java中使用BufferedInputStream跳过特定字节的问题,以及内部类在不同上下文中访问的限制。面对SWUST平台上的挑战,文章深入分析了skipNbytes方法的缺失及替代方案,并解释了No enclosing instance of type Main is accessible错误的原因与解决办法。

可能是jdk版本问题,SWUST上面的没有skipNbytes();这个方法。错了一百遍。。。。。,虽然有这个题用这个跳过\r\n也是错的,因为第二行还有其他的空白符且在要被搜索的字符之前
用bufferedinputStream的话它是一次性将所有输入读入缓冲区,虽然还不知道缓冲区是个啥,
** No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).这句话是提示内部类要在外部类的main里面使用内部类要定义成static,因为静态方法不可以直接调用动态方法 **
在这里插入图片描述

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedInputStream in = new BufferedInputStream(System.in);
//		char c = (char) in.read();
//		node t = new node(c);
		node t = new node().creattree(in);
		//in.skipNBytes(2L);
//		in.close();
		char c = (char) in.read();
		while(c==' '||c=='\n'||c=='\r') c = (char) in.read();
		
		t.find(t, c);
		
	}

	public static class node {
		char ch;
		node R, L;

		node(char ch) {
			super();
			this.ch = ch;
		}
		node() {
		}

		public void find(node t, char c) {
			if (t == null)
				return;
			if (t.ch == c) {
				if (t.L == null)
					System.out.print("L:#");
				else
					System.out.print("L:" + t.L.ch);
				if (t.R == null)
					System.out.print(",R:#");
				else
					System.out.print(",R:" + t.R.ch);
				return;
			}
			find(t.L, c);
			find(t.R, c);
		}

		public node creattree(BufferedInputStream in) throws IOException {
			char c = (char) in.read();
			node t;
			if (c == '#') {
				return t = null;
			} else {
				t = new node(c);
			}
			t.L = creattree(in);
			t.R = creattree(in);
			return t;
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值