树形结构的输入BufferedReader
输入描述:
第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
输出描述:
输出两行整数分别表示按两种标准的边界节点。
示例
输入
16 1
1 2 3
2 0 4
4 7 8
7 0 0
8 0 11
11 13 14
13 0 0
14 0 0
3 5 6
5 9 10
10 0 0
9 12 0
12 15 16
15 0 0
16 0 0
6 0 0
输出
1 2 4 7 11 13 14 15 16 12 10 6 3
1 2 4 7 13 14 15 16 10 6 3
```java
import java.io.*;
import java.util.*;
public class Main {
static class Node{
Node left;
Node right;
int val;
Node(int val){
this.val = val;
}
}
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] strs = reader.readLine().split(" ");
int n = Integer.valueOf(strs[0]);
Node root = new Node(Integer.valueOf(strs[1]));
HashMap<Integer,Node> map = new HashMap<>();
map.put(root.val,root);
Node left,right,parent;
//构建二叉树
for(int i=0;i<n;i++) {
String[] content = reader.readLine().split(" ");
parent = map.get(Integer.valueOf(content[0]));
int leftValue = Integer.valueOf(content[1]);
int rightValue = Integer.valueOf(content[2]);
left = leftValue == 0 ? null : new Node(leftValue);
right = rightValue == 0 ? null : new Node(rightValue);
parent.left = left;
parent.right = right;
map.put(leftValue,left);
map.put(rightValue,right);
}
//---------------至此已经生成二叉树 root
}
}
输入描述:
第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
ps:节点的标号就是节点的值
输出描述:
第一行输出该二叉树是否为搜索二叉树的答案