本题考查二叉搜索树的构建,与树的先序遍历、后序遍历。
思路:按照二叉搜索树规则构建二叉树,对该树进行先序与镜像先序遍历,若两个顺序的其中一个与题目给的相符,则输出YES,并输出其后序遍历结果。
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scaner = new Scanner(System.in);
Tree tree = new Tree();
int round = scaner.nextInt();
String ori = "";
for(int i = 0 ; i < round ; i++) {
int temp = scaner.nextInt();
ori += temp + " ";
tree.insert(temp);
}
scaner.close();
ori = ori.trim();
String pre = tree.preOrder(tree.root,"").trim();
String mPre = tree.mPreOrder(tree.root,"").trim();
if(ori.equals(pre)) {
System.out.println("YES");
String postResult = tree.postOrder(tree.root,"").trim();
System.out.println(postResult);
}
else if(ori.equals(mPre)) {
System.out.println("YES");
String mPostResult = tree.mPostOrder(tree.root,"").trim();
System.out.println(mPostResult);
}
else {
System.out.println("NO");
}
}
}
class Node{
private int key;
private Node leftChild;
private Node rightChild;
public Node(int key) {
this.key = key;
leftChild = null;
rightChild = null;
}
public int getKey() {
return key;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getLeftChild() {
return leftChild;
}
public Node getRightChild() {
return rightChild;
}
}
class Tree{
Node root;
public Tree() {
root = null;
}
public void insert(int key) {
if(root == null) {
root = new Node(key);
}
else {
Node temp = root;
while(true) {
if(key < temp.getKey()) {
if(temp.getLeftChild() == null) {
temp.setLeftChild(new Node(key));
break;
}
else
temp = temp.getLeftChild();
}
else {
if(temp.getRightChild() == null) {
temp.setRightChild(new Node(key));
break;
}
else
temp = temp.getRightChild();
}
}
}
}
public String preOrder(Node root , String result) {
result += root.getKey() + " ";
Node leftChild = root.getLeftChild();
if(leftChild != null) {
result = preOrder(leftChild , result);
}
Node rightChild = root.getRightChild();
if(rightChild != null) {
result = preOrder(rightChild , result);
}
return result;
}
public String postOrder(Node root , String result) {
Node leftChild = root.getLeftChild();
if(leftChild != null) {
result = postOrder(leftChild , result);
}
Node rightChild = root.getRightChild();
if(rightChild != null) {
result = postOrder(rightChild , result);
}
result += root.getKey() + " ";
return result;
}
public String mPreOrder(Node root , String result) {
result += root.getKey() + " ";
Node rightChild = root.getRightChild();
if(rightChild != null) {
result = mPreOrder(rightChild , result);
}
Node leftChild = root.getLeftChild();
if(leftChild != null) {
result = mPreOrder(leftChild , result);
}
return result;
}
public String mPostOrder(Node root , String result) {
Node rightChild = root.getRightChild();
if(rightChild != null) {
result = mPostOrder(rightChild , result);
}
Node leftChild = root.getLeftChild();
if(leftChild != null) {
result = mPostOrder(leftChild , result);
}
result += root.getKey() + " ";
return result;
}
}
参考网址:
- https://blog.csdn.net/coder__666/article/details/80349039
- https://blog.csdn.net/a845717607/article/details/86674999(C++版)